From 9fd3cd840645faf7a525e4e72dcdda3ad6a00c43 Mon Sep 17 00:00:00 2001 From: leGit-y Date: Mon, 6 Nov 2023 01:53:10 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]=20=EC=84=9C=EB=B9=84=EC=8A=A4=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=95=84=EC=9B=83=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prisma/schema.prisma | 2 +- src/controller/authController.ts | 80 +++++++++++++++++-------------- src/repository/tokenRepository.ts | 10 +++- src/router/authRouter.ts | 13 ++--- src/service/authService.ts | 9 ++-- 5 files changed, 64 insertions(+), 50 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 95b9ae7..3858ed2 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -64,6 +64,6 @@ model worry { model token { user_id Int @id - refresh_token String @unique @db.VarChar(150) + refresh_token String @db.VarChar(150) user user @relation(fields: [user_id], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "token_user_id_fk") } diff --git a/src/controller/authController.ts b/src/controller/authController.ts index a3358a9..9ebdb33 100644 --- a/src/controller/authController.ts +++ b/src/controller/authController.ts @@ -7,6 +7,7 @@ import qs from "qs"; import authService from "../service/authService"; + // 서버에서 테스트용 카카오 토큰을 받기 위해 쓰일, 인가코드를 받기 위한 함수 const kakaoLogin_getAuthorizedCode = async (req: Request, res: Response, next: NextFunction) => { try{ @@ -109,35 +110,6 @@ const kakaoLogin_getAuthorizedCode = async (req: Request, res: Response, next: N } } - - const kakaoLogout =async (req: Request, res:Response, next:NextFunction) => { - try{ - const { accessToken } = req.body; - - const response = await axios({ - method: 'POST', - url: 'https://kapi.kakao.com/v1/user/logout', - headers:{ - 'Authorization': `Bearer ${accessToken}`, - } - }) - - return res.status(sc.OK).send(success(statusCode.OK, rm.KAKAO_LOGOUT_SUCCESS, response.data.msg)); - - }catch(error:any){ - - //토큰이 유효하지 않은 경우 - if(error.response.data.msg == "this access token does not exist"){ - return res.status(sc.UNAUTHORIZED).send(fail(sc.UNAUTHORIZED, rm.INVALID_TOKEN)); - } - console.log(error) - - return res.status(error.response.status).send(fail(error.response.status, error.response.data.msg)); - - } - - } - const appleLogin =async (req: Request, res:Response, next:NextFunction) => { try { @@ -158,20 +130,58 @@ const kakaoLogin_getAuthorizedCode = async (req: Request, res: Response, next: N // - 기존회원이 로그인한 경우 return res.status(sc.OK).send(success(sc.OK, rm.LOGIN_SUCCESS, data.result)); - - } catch (err) { - console.log("Err", err) + } catch (err) { + console.log("Err", err) + } } - + + const serviceLogout =async (req: Request, res:Response, next:NextFunction) => { + try{ + const { userId } = req.body; + await authService.serviceLogout(userId); + + + return res.status(sc.OK).send(success(sc.OK, rm.LOGOUT_SUCCESS)); + + }catch(error){ + next(error); + } } - + + // const kakaoLogout =async (req: Request, res:Response, next:NextFunction) => { + // try{ + // const { accessToken } = req.body; + + // const response = await axios({ + // method: 'POST', + // url: 'https://kapi.kakao.com/v1/user/logout', + // headers:{ + // 'Authorization': `Bearer ${accessToken}`, + // } + // }) + + // return res.status(sc.OK).send(success(statusCode.OK, rm.KAKAO_LOGOUT_SUCCESS, response.data.msg)); + + // }catch(error:any){ + + // //토큰이 유효하지 않은 경우 + // if(error.response.data.msg == "this access token does not exist"){ + // return res.status(sc.UNAUTHORIZED).send(fail(sc.UNAUTHORIZED, rm.INVALID_TOKEN)); + // } + // console.log(error) + + // return res.status(error.response.status).send(fail(error.response.status, error.response.data.msg)); + + // } + + // } export default{ kakaoLogin_getAuthorizedCode, kakaoLogin_getToken, kakaoLogin, - kakaoLogout, appleLogin, + serviceLogout, } \ No newline at end of file diff --git a/src/repository/tokenRepository.ts b/src/repository/tokenRepository.ts index 94d6adf..6c3ed03 100644 --- a/src/repository/tokenRepository.ts +++ b/src/repository/tokenRepository.ts @@ -44,12 +44,20 @@ const updateRefreshTokenById = async (userId: number, token:string) => { }); }; +const deleteRefreshTokenById = async (userId: number) => { + return await prisma.token.delete({ + where: { + user_id: userId + } + }); +}; + export default { createRefreshToken, findRefreshTokenById, findIdByRefreshToken, updateRefreshTokenById, - + deleteRefreshTokenById } \ No newline at end of file diff --git a/src/router/authRouter.ts b/src/router/authRouter.ts index 938b441..7a5a813 100644 --- a/src/router/authRouter.ts +++ b/src/router/authRouter.ts @@ -20,15 +20,6 @@ router.post( authController.kakaoLogin ) -router.post("/kakao/logout", - [ - body('accessToken').notEmpty().withMessage("body 에 'accessToken' 값이 존재하지 않습니다"), - ], - validate, - authController.kakaoLogout -) - - router.post("/apple/login", [ body('identityToken').notEmpty().withMessage("body 에 'identityToken' 값이 존재하지 않습니다"), @@ -39,6 +30,10 @@ router.post("/apple/login", authController.appleLogin ) +router.post("/logout", + auth, + authController.serviceLogout +) router.post("/token/refresh", [ diff --git a/src/service/authService.ts b/src/service/authService.ts index 2def89c..85062ac 100644 --- a/src/service/authService.ts +++ b/src/service/authService.ts @@ -123,11 +123,12 @@ const serviceLogin = async (provider:string, user:any) => { } - const serviceLogout = async (userId:number) => { - - - // await tokenRepository.updateRefreshTokenById(accessToken, refreshToken); +const serviceLogout = async (userId:number) => { + const deleted = await tokenRepository.deleteRefreshTokenById(userId); + if(!deleted){ + throw new ClientException("refresh token delete fail"); + } }