Skip to content

Commit

Permalink
Merge pull request #94 from boostcampwm-2024/be/fix/oauth_redirect_error
Browse files Browse the repository at this point in the history
[BE/fix] oauth 리다이렉트 문제 수정
  • Loading branch information
HBLEEEEE authored Nov 25, 2024
2 parents 28bf1f9 + cc8f328 commit b7b5403
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
8 changes: 8 additions & 0 deletions .Dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
node_modules/
dist
*.log
.git
.github
.gitignore
Dockerfile
build/
16 changes: 12 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Node.js 20 이미지 설정
FROM node:20
# 1단계: 빌드 스테이지 (멀티 스테이지 빌드)
FROM node:20-alpine AS builder

# pnpm 설치
RUN npm install -g pnpm
Expand All @@ -13,15 +13,23 @@ RUN pnpm install

# 프론트엔드 파일 복사 및 빌드
WORKDIR /app/apps/frontend
COPY apps/frontend ./
COPY apps/frontend ./
RUN pnpm install
RUN pnpm run build

# 백엔드 파일 복사 및 의존성 설치
WORKDIR /app/apps/backend
COPY apps/backend ./
COPY apps/backend ./
RUN pnpm install

# 2단계: 실행 이미지
FROM node:20-alpine

# 작업 디렉토리 설정 및 복사
WORKDIR /app
RUN npm install -g pnpm
COPY --from=builder /app /app

# 프론트엔드와 백엔드 포트 노출 설정
EXPOSE 3000 8080

Expand Down
10 changes: 7 additions & 3 deletions apps/backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Body, Controller, Get, Patch, Post, Req, UseGuards } from '@nestjs/common';
import { Request } from 'express';
import { Request, Response } from 'express';
import { ApiOperation } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { AuthService } from './auth.service';
Expand Down Expand Up @@ -52,7 +52,9 @@ export class AuthController {
@Get('google')
@UseGuards(AuthGuard('google'))
@ApiOperation({ summary: '구글 로그인 API' })
async googleLogin() {}
async googleLogin(@Req() response: Response) {
return this.authService.googleRedirect(response);
}

@Get('google/redirect')
@UseGuards(AuthGuard('google'))
Expand All @@ -66,7 +68,9 @@ export class AuthController {
@Get('kakao')
@UseGuards(AuthGuard('kakao'))
@ApiOperation({ summary: '카카오 로그인 API' })
async kakaoLogin() {}
async kakaoLogin(@Req() response: Response) {
return this.authService.kakaoRedirect(response);
}

@Get('kakao/redirect')
@UseGuards(AuthGuard('kakao'))
Expand Down
13 changes: 13 additions & 0 deletions apps/backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import { GoogleLoginDto } from './dto/googleLogin.dto';
import { KakaoLoginDto } from './dto/kakaoLogin.dto';
import { RedisClientType } from 'redis';
import { Nullable, Optional } from '../global/utils/dataCustomType';
import { Response } from 'express';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class AuthService {
constructor(
private readonly databaseService: DatabaseService,
private readonly jwtService: JwtService,
private configService: ConfigService,
@Inject('REDIS_CLIENT') private readonly redisClient: RedisClientType
) {}

Expand Down Expand Up @@ -110,11 +113,21 @@ export class AuthService {
return this.SocialLogin(email, name);
}

async googleRedirect(response: Response) {
const googleAuthUrl = `https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_uri=${this.configService.get<string>('GOOGLE_CALLBACK_URL')}&client_id=${this.configService.get<string>('GOOGLE_CLIENT_ID')}&scope=email%20profile`;
return response.redirect(googleAuthUrl);
}

async kakaoLogin(kakaoLoginDto: KakaoLoginDto) {
const { email, nickname } = kakaoLoginDto;
return this.SocialLogin(email, nickname);
}

async kakaoRedirect(response: Response) {
const kakaoAuthUrl = `https://kauth.kakao.com/oauth/authorize?response_type=code&client_id=${this.configService.get<string>('KAKAO_REST_API_KEY')}&redirect_uri=${this.configService.get<string>('KAKAO_CALLBACK_URL')}&scope=profile,email`;
return response.redirect(kakaoAuthUrl);
}

async logout(token: Optional<string>) {
if (!token) throw new HttpException('토큰이 필요합니다.', HttpStatus.BAD_REQUEST);

Expand Down

0 comments on commit b7b5403

Please sign in to comment.