Skip to content

Commit 0b723b3

Browse files
committed
feat: BE - kakao oauth로 시작하기 구현
1 parent e195df5 commit 0b723b3

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

backend/src/auth/service/oauth/kakao-oauth.service.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {
2+
AUTHORIZATION_TOKEN_TYPE,
23
KAKAO_ACCESS_TOKEN_URL,
34
KAKAO_AUTHORIZE_PAGE_URL,
5+
KAKAO_PROFILE_API_URL,
46
OAUTH_CALLBACK_URL,
57
OAUTH_TYPE,
68
} from '@constant';
@@ -36,7 +38,19 @@ export class OauthKakaoService implements OauthService {
3638
console.log(access_token);
3739
return access_token;
3840
}
39-
getSocialInfoByAccessToken(accessToken: string): Promise<UserSocialInfo> {
40-
throw new Error('Method not implemented.');
41+
42+
/**
43+
* @link https://developers.kakao.com/docs/latest/ko/kakaologin/rest-api#req-user-info
44+
* @param accessToken
45+
* @returns userInfo
46+
*/
47+
async getSocialInfoByAccessToken(accessToken: string): Promise<UserSocialInfo> {
48+
const headers = { Authorization: `${AUTHORIZATION_TOKEN_TYPE} ${accessToken}` };
49+
const res = await axios.get(KAKAO_PROFILE_API_URL, { headers }).then((res) => res.data);
50+
51+
const user = res['kakao_account'] as UserSocialInfo;
52+
user.id = res.id;
53+
user.oauthType = OAUTH_TYPE.KAKAO;
54+
return user;
4155
}
4256
}

backend/src/auth/service/oauth/naver-oauth.service.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@ export class OauthNaverService implements OauthService {
2222
].join('/');
2323

2424
getSocialUrl(): string {
25-
const queryString = `?response_type=token&client_id=${this.clientId}&redirect_uri=${this.callbackUrl}`;
25+
const queryString = `?response_type=code&client_id=${this.clientId}&redirect_uri=${this.callbackUrl}`;
2626
return NAVER_AUTHORIZE_PAGE_URL + queryString;
2727
}
2828

2929
async getAccessTokenByAuthorizationCode(authorizationCode: string): Promise<string> {
30-
if (!authorizationCode) throw new Error();
31-
3230
const queryString =
3331
`?grant_type=authorization_code` +
3432
`&client_id=${this.clientId}&client_secret=${this.clientSecret}` +
@@ -57,7 +55,8 @@ export class OauthNaverService implements OauthService {
5755
if (res.resultcode !== '00') {
5856
throw new Error(res.message);
5957
}
60-
61-
return res.response;
58+
const user = res.response as UserSocialInfo;
59+
user.oauthType = OAUTH_TYPE.NAVER;
60+
return user;
6261
}
6362
}

backend/src/constant/auth.constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ export const NAVER_PROFILE_API_URL = 'https://openapi.naver.com/v1/nid/me';
1616
// KAKAO
1717
export const KAKAO_AUTHORIZE_PAGE_URL = 'https://kauth.kakao.com/oauth/authorize';
1818
export const KAKAO_ACCESS_TOKEN_URL = 'https://kauth.kakao.com/oauth/token';
19+
export const KAKAO_PROFILE_API_URL = 'https://kapi.kakao.com/v2/user/me';

0 commit comments

Comments
 (0)