From 539f9c95d5ae72db78a8383ca4ceae6dc3bfaa7d Mon Sep 17 00:00:00 2001 From: NicoDora Date: Fri, 13 Oct 2023 13:00:14 +0900 Subject: [PATCH] =?UTF-8?q?refactor(#10):=20=EC=B9=B4=EC=B9=B4=EC=98=A4=20?= =?UTF-8?q?=ED=9A=8C=EC=9B=90=ED=83=88=ED=87=B4=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/auth/controllers/auth.controller.ts | 12 ++-------- src/auth/services/auth.service.ts | 29 +++++++++++++++++-------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/auth/controllers/auth.controller.ts b/src/auth/controllers/auth.controller.ts index 57596b1..332b1b3 100644 --- a/src/auth/controllers/auth.controller.ts +++ b/src/auth/controllers/auth.controller.ts @@ -89,17 +89,9 @@ export class AuthController { async kakaoUnlink(@Headers('access_token') accessToken: string) { const userId = await this.tokenService.decodeToken(accessToken); const tokens = await this.tokenService.getUserTokens(userId); - let kakaoAccessToken = tokens[0].socialAccessToken; - - const checkValidKakaoToken = await this.tokenService.checkValidKakaoToken(kakaoAccessToken); - - if (checkValidKakaoToken === 401) { - const kakaoRefreshToken = tokens[0].socialRefreshToken; - const newKakaoToken = await this.tokenService.getNewKakaoToken(kakaoRefreshToken); - kakaoAccessToken = newKakaoToken.access_token; - } + const dbAccessToken = tokens[0].socialAccessToken; await this.tokenService.deleteTokens(userId); - return await this.authService.kakaoUnlink(kakaoAccessToken); + return await this.authService.kakaoUnlink(accessToken, dbAccessToken); } @ApiOperation({ summary: '네이버 로그아웃 API', description: '네이버 로그아웃 API' }) diff --git a/src/auth/services/auth.service.ts b/src/auth/services/auth.service.ts index 80893c9..caa2753 100644 --- a/src/auth/services/auth.service.ts +++ b/src/auth/services/auth.service.ts @@ -184,16 +184,27 @@ export class AuthService { } } - async kakaoUnlink(accessToken: string) { - const kakaoUnlinkUrl = 'https://kapi.kakao.com/v1/user/unlink'; - const kakaoUnlinkHeader = { - headers: { - Authorization: `Bearer ${accessToken}`, - }, - }; + async kakaoUnlink(accessToken: string, dbAccessToken: string) { + try { + const checkValidKakaoToken = await this.tokenService.checkValidKakaoToken(accessToken); + if (checkValidKakaoToken === 401) { + const newKakaoToken = await this.tokenService.getNewKakaoToken(dbAccessToken); + accessToken = newKakaoToken.access_token; + } - axios.post(kakaoUnlinkUrl, {}, kakaoUnlinkHeader); - return { message: "카카오 연결 끊기가 완료되었습니다." }; + const kakaoUnlinkUrl = 'https://kapi.kakao.com/v1/user/unlink'; + const kakaoUnlinkHeader = { + headers: { + Authorization: `Bearer ${accessToken}`, + }, + }; + + axios.post(kakaoUnlinkUrl, {}, kakaoUnlinkHeader); + return { message: "카카오 연결 끊기가 완료되었습니다." }; + } catch (error) { + console.log(error); + throw new HttpException('알 수 없는 오류가 발생했습니다.', HttpStatus.INTERNAL_SERVER_ERROR) + } } async naverUnlink(accessToken: string) {