Skip to content

Commit 0348e1f

Browse files
authored
Merge pull request #2 from ccaaffee/kakaologin
feat: modify kakao login logic
2 parents 590a849 + 80c1570 commit 0348e1f

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

src/auth/auth.controller.ts

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,65 @@
1-
import { Controller, Get, HttpStatus, Query, Res } from '@nestjs/common';
1+
import {
2+
Body,
3+
Controller,
4+
Get,
5+
HttpStatus,
6+
Post,
7+
Query,
8+
Res,
9+
} from '@nestjs/common';
210
import { AuthService } from './auth.service';
311
import { Response } from 'express';
4-
import { ApiOperation, ApiTags } from '@nestjs/swagger';
12+
import { ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
513

614
@ApiTags('Auth')
715
@Controller('auth')
816
export class AuthController {
917
constructor(private readonly authService: AuthService) {}
1018

19+
@ApiOperation({
20+
summary: 'kakao login with accessToken from front',
21+
description:
22+
'(only for staging server)kakao accessToken을 입력하면 cafe-search 서비스의 accessToken을 반환합니다.',
23+
})
24+
@ApiBody({
25+
schema: {
26+
type: 'object',
27+
properties: {
28+
accessToken: {
29+
type: 'string',
30+
description: 'Kakao access token',
31+
example: 'your-kakao-access-token',
32+
},
33+
},
34+
},
35+
})
36+
@Post('kakao/signin')
37+
async kakaoLoginWithAccessToken(
38+
@Body('accessToken') accessToken: string,
39+
@Res() res: Response,
40+
) {
41+
try {
42+
const jwtToken =
43+
await this.authService.kakaoLoginWithAccessToken(accessToken);
44+
45+
return res.status(HttpStatus.OK).json({
46+
message: 'Kakao Login Success',
47+
token: jwtToken,
48+
});
49+
} catch (error) {
50+
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
51+
message: 'Kakao Login Failed',
52+
error: error.message,
53+
});
54+
}
55+
}
56+
1157
@ApiOperation({
1258
summary: 'redirect to kakao login page',
1359
description: 'API 요청 시 카카오 로그인 페이지로 리다이렉트됩니다.',
1460
})
15-
@Get('kakao')
16-
async kakaoLogin(@Res() res: Response) {
61+
@Get('kakao/login')
62+
async getKakaoLoginUri(@Res() res: Response) {
1763
const kakaoAuthUrl = this.authService.getKakaoAuthUrl();
1864

1965
return res.redirect(kakaoAuthUrl);
@@ -22,7 +68,7 @@ export class AuthController {
2268
@ApiOperation({
2369
summary: 'callback api for kakao login',
2470
description:
25-
'kakao login page에 로그인 후 받은 authorization code와 함께 API 요청 시 access_token을 반환합니다.',
71+
'(only for staging server)kakao login page에 로그인 후 받은 authorization code와 함께 API 요청 시 access_token을 반환합니다.',
2672
})
2773
@Get('kakao/callback')
2874
async kakaoLoginCallback(@Query('code') code: string, @Res() res: Response) {

src/auth/auth.service.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export class AuthService {
2020
this.kakaoRestApiKey = this.configService.get<string>('KAKAO_REST_API_KEY');
2121
this.kakaoRedirectUri =
2222
this.configService.get<string>('KAKAO_REDIRECT_URI');
23-
this.kakaoAuthUri = this.configService.get<string>('KAKAO_AUTH_URL');
24-
this.kakaoTokenUri = this.configService.get<string>('KAKAO_TOKEN_URL');
23+
this.kakaoAuthUri = this.configService.get<string>('KAKAO_AUTH_URI');
24+
this.kakaoTokenUri = this.configService.get<string>('KAKAO_TOKEN_URI');
2525
}
2626

2727
/**
@@ -35,7 +35,11 @@ export class AuthService {
3535
response_type: 'code',
3636
}).toString();
3737

38-
return `${baseUrl}?${queryParams}`;
38+
const url = `${baseUrl}?${queryParams}`;
39+
40+
console.log(url);
41+
42+
return url;
3943
}
4044

4145
/**
@@ -46,6 +50,9 @@ export class AuthService {
4650

4751
const accessToken = tokenReponse.access_token;
4852

53+
console.log('kakao accesstoken');
54+
console.log(accessToken);
55+
4956
// 2) 발급한 access token으로 유저정보 요청 to 카카오 로그인 서버
5057
const kakaoUserInfo = await this.getKakaoUserInfo(accessToken);
5158

@@ -65,6 +72,23 @@ export class AuthService {
6572
return jwtToken;
6673
}
6774

75+
async kakaoLoginWithAccessToken(accessToken: string): Promise<string> {
76+
const kakaoUserInfo = await this.getKakaoUserInfo(accessToken);
77+
78+
const kakaoId = kakaoUserInfo.id.toString();
79+
80+
let user = await this.userService.findByKakaoId(kakaoId);
81+
82+
if (!user) {
83+
user = await this.userService.createUser(kakaoId);
84+
}
85+
86+
const payload = { sub: user.uuid };
87+
const jwtToken = this.jwtService.sign(payload);
88+
89+
return jwtToken;
90+
}
91+
6892
/**
6993
* 카카오 Access Token 발급
7094
*/

0 commit comments

Comments
 (0)