Skip to content

Commit

Permalink
Merge pull request #336 from Quickchive/feat/QA-335/kakao-auth
Browse files Browse the repository at this point in the history
[QA-335] 카카오 로그인 api 생성
  • Loading branch information
stae1102 authored Jul 7, 2024
2 parents 5eca7ba + 7d08edc commit c10e18f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/auth/dtos/kakao-login.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { CreateOAuthDto } from './oauth-login.dto';

export class KakaoLoginDto extends CreateOAuthDto {}
3 changes: 3 additions & 0 deletions src/auth/dtos/oauth-login.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class CreateOAuthDto {
authorizationToken: string;
}
3 changes: 3 additions & 0 deletions src/auth/dtos/request/kakao-login.request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { OAuthLoginRequest } from './oauth-login.request.dto';

export class KakaoLoginRequest extends OAuthLoginRequest {}
8 changes: 8 additions & 0 deletions src/auth/dtos/request/oauth-login.request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';

export class OAuthLoginRequest {
@ApiProperty({
description: 'OAuth 인증 토큰(ex. kakao access token, google id token)',
})
authorizationToken: string;
}
10 changes: 10 additions & 0 deletions src/auth/oauth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { OAuthService } from './oauth.service';
import { googleUserInfo } from './dtos/google.dto';
import { LoginOutput } from './dtos/login.dto';
import { ErrorOutput } from '../common/dtos/output.dto';
import { KakaoLoginRequest } from './dtos/request/kakao-login.request.dto';

@Controller('oauth')
@ApiTags('OAuth')
Expand Down Expand Up @@ -129,4 +130,13 @@ export class OAuthController {
async appleLogin(@Query('code') code: string): Promise<LoginOutput> {
return this.oauthService.appleLogin(code);
}

@ApiOperation({
summary: '카카오 로그인 요청',
description: 'accessToken을 받아 카카오 로그인을 요청합니다.',
})
@Post('kakao')
async createOneWithKakao(@Body() kakaoLoginRequest: KakaoLoginRequest) {
return this.oauthService.createOneWithKakao(kakaoLoginRequest);
}
}
36 changes: 36 additions & 0 deletions src/auth/oauth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import * as CryptoJS from 'crypto-js';
import { CookieOptions } from 'express';
import { RedisService } from '../infra/redis/redis.service';
import { REFRESH_TOKEN_KEY } from './constants';
import { KakaoLoginRequest } from './dtos/request/kakao-login.request.dto';
import { KakaoLoginDto } from './dtos/kakao-login.dto';

@Injectable()
export class OAuthService {
Expand Down Expand Up @@ -118,6 +120,40 @@ export class OAuthService {
}
}

async createOneWithKakao({ authorizationToken }: KakaoLoginDto) {
try {
const { userInfo } = await this.oauthUtil.getKakaoUserInfo(
authorizationToken,
);

const email = userInfo.kakao_account.email;
if (!email) {
throw new BadRequestException('Please Agree to share your email');
}

let user = await this.userRepository.findOneByEmail(email);

// 회원가입인 경우 기본 카테고리 생성 작업 진행
if (!user) {
user = new User();
user.email = email;
user.name = userInfo.kakao_account.profile.nickname;
user.profileImage = userInfo.kakao_account.profile?.profile_image_url;
user.password = this.encodePasswordFromEmail(
email,
process.env.KAKAO_JS_KEY,
);

await this.userRepository.createOne(user);
await this.categoryRepository.createDefaultCategories(user);
}

return this.oauthLogin(user.email);
} catch (e) {
throw e;
}
}

// Login with Google account info
async googleOauth({
email,
Expand Down

0 comments on commit c10e18f

Please sign in to comment.