Skip to content

Commit

Permalink
[FEAT] 서비스 로그아웃하기
Browse files Browse the repository at this point in the history
  • Loading branch information
leGit-y committed Nov 5, 2023
1 parent db321eb commit 9fd3cd8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 50 deletions.
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
80 changes: 45 additions & 35 deletions src/controller/authController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import qs from "qs";
import authService from "../service/authService";



// 서버에서 테스트용 카카오 토큰을 받기 위해 쓰일, 인가코드를 받기 위한 함수
const kakaoLogin_getAuthorizedCode = async (req: Request, res: Response, next: NextFunction) => {
try{
Expand Down Expand Up @@ -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 {
Expand All @@ -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,

}
10 changes: 9 additions & 1 deletion src/repository/tokenRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
13 changes: 4 additions & 9 deletions src/router/authRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' 값이 존재하지 않습니다"),
Expand All @@ -39,6 +30,10 @@ router.post("/apple/login",
authController.appleLogin
)

router.post("/logout",
auth,
authController.serviceLogout
)

router.post("/token/refresh",
[
Expand Down
9 changes: 5 additions & 4 deletions src/service/authService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

}

Expand Down

0 comments on commit 9fd3cd8

Please sign in to comment.