Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] 서비스 로그아웃하기 #70

Merged
merged 2 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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