diff --git a/apps/backend/src/auth/auth.controller.ts b/apps/backend/src/auth/auth.controller.ts index 482a955..91836af 100644 --- a/apps/backend/src/auth/auth.controller.ts +++ b/apps/backend/src/auth/auth.controller.ts @@ -8,6 +8,7 @@ import { signUpResponseDecorator } from './decorator/signup.decorator'; import { loginResponseDecorator } from './decorator/login.decorator'; import { AuthGuard } from '@nestjs/passport'; import { GoogleLoginDto } from './dto/googleLogin.dto'; +import { KakaoLoginDto } from './dto/kakaoLogin.dto'; import { oauthResponseDecorator } from './decorator/oauth.decorator'; @Controller('api/auth') @@ -37,10 +38,24 @@ export class AuthController { @Get('google/redirect') @UseGuards(AuthGuard('google')) + @ApiOperation({ summary: '구글 로그인 리다이렉션 API' }) @oauthResponseDecorator() - @ApiOperation({ summary: '구글 로그인 후 리다이렉션 API' }) async googleRedirect(@Req() googleLoginDto: GoogleLoginDto) { const tokens = await this.authService.googleLogin(googleLoginDto); return successhandler(successMessage.LOGIN_SUCCESS, tokens); } + + @Get('kakao') + @UseGuards(AuthGuard('kakao')) + @ApiOperation({ summary: '카카오 로그인 API' }) + async kakaoLogin() {} + + @Get('kakao/redirect') + @UseGuards(AuthGuard('kakao')) + @ApiOperation({ summary: '카카오 로그인 리다이렉션 API' }) + @oauthResponseDecorator() + async kakaoRedirect(@Req() kakaoLoginDto: KakaoLoginDto) { + const tokens = await this.authService.kakaoLogin(kakaoLoginDto); + return successhandler(successMessage.LOGIN_SUCCESS, tokens); + } } diff --git a/apps/backend/src/auth/auth.module.ts b/apps/backend/src/auth/auth.module.ts index 0725659..e065be5 100644 --- a/apps/backend/src/auth/auth.module.ts +++ b/apps/backend/src/auth/auth.module.ts @@ -6,6 +6,7 @@ import { JwtModule } from '@nestjs/jwt'; import { ConfigModule, ConfigService } from '@nestjs/config'; import { PassportModule } from '@nestjs/passport'; import { GoogleStrategy } from './strategies/google.strategy'; +import { KakaoStrategy } from './strategies/kakao.strategy'; @Module({ imports: [ @@ -21,7 +22,7 @@ import { GoogleStrategy } from './strategies/google.strategy'; }) }) ], - providers: [AuthService, GoogleStrategy], + providers: [AuthService, GoogleStrategy, KakaoStrategy], controllers: [AuthController] }) export class AuthModule {} diff --git a/apps/backend/src/auth/auth.service.ts b/apps/backend/src/auth/auth.service.ts index e416aed..e2d5c03 100644 --- a/apps/backend/src/auth/auth.service.ts +++ b/apps/backend/src/auth/auth.service.ts @@ -6,6 +6,7 @@ import { authQueries } from './auth.queries'; import { LoginDto } from './dto/login.dto'; import { JwtService } from '@nestjs/jwt'; import { GoogleLoginDto } from './dto/googleLogin.dto'; +import { KakaoLoginDto } from './dto/kakaoLogin.dto'; @Injectable() export class AuthService { @@ -69,12 +70,12 @@ export class AuthService { return { accessToken, refreshToken }; } - async googleLogin(googleLoginDto: GoogleLoginDto) { - const { email, name } = googleLoginDto; + async loginWithSocialMedia(email: string, nickname: string) { const existingUser = await this.databaseService.query(authQueries.findByEmailQuery, [email]); + if (!existingUser) { const hashedPassword = await bcrypt.hash('default', 10); - await this.databaseService.query(authQueries.signUpQuery, [email, hashedPassword, name]); + await this.databaseService.query(authQueries.signUpQuery, [email, hashedPassword, nickname]); } const member = await this.databaseService.query(authQueries.findByEmailQuery, [email]); const payload = { @@ -86,4 +87,14 @@ export class AuthService { const refreshToken = this.jwtService.sign(payload, { expiresIn: '7d' }); return { accessToken, refreshToken }; } + + async googleLogin(googleLoginDto: GoogleLoginDto) { + const { email, name } = googleLoginDto; + return this.loginWithSocialMedia(email, name); + } + + async kakaoLogin(kakaoLoginDto: KakaoLoginDto) { + const { email, nickname } = kakaoLoginDto; + return this.loginWithSocialMedia(email, nickname); + } } diff --git a/apps/backend/src/auth/dto/kakaoLogin.dto.ts b/apps/backend/src/auth/dto/kakaoLogin.dto.ts new file mode 100644 index 0000000..061dec6 --- /dev/null +++ b/apps/backend/src/auth/dto/kakaoLogin.dto.ts @@ -0,0 +1,4 @@ +export class KakaoLoginDto { + email: string; + nickname: string; +} diff --git a/apps/backend/src/auth/strategies/kakao.strategy.ts b/apps/backend/src/auth/strategies/kakao.strategy.ts new file mode 100644 index 0000000..84a4092 --- /dev/null +++ b/apps/backend/src/auth/strategies/kakao.strategy.ts @@ -0,0 +1,28 @@ +import { Injectable } from '@nestjs/common'; +import { PassportStrategy } from '@nestjs/passport'; +import { Strategy } from 'passport-kakao'; +import { ConfigService } from '@nestjs/config'; + +@Injectable() +export class KakaoStrategy extends PassportStrategy(Strategy, 'kakao') { + constructor(private configService: ConfigService) { + super({ + clientID: configService.get('KAKAO_REST_API_KEY'), + callbackURL: configService.get( + 'KAKAO_CALLBACK_URL', + 'http://localhost:3000/auth/kakao/redirect' + ) + }); + } + + async validate( + accessToken: string, + refreshToken: string, + profile: { nickname: string; email: string } + ) { + const { nickname, email } = profile; + + const member = { email, nickname }; + return member; + } +}