Skip to content

Commit

Permalink
πŸ› 토큰 μ—λŸ¬ μˆ˜μ •
Browse files Browse the repository at this point in the history
  • Loading branch information
jjikky committed Jun 29, 2024
1 parent bcf6efb commit aecb42d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 42 deletions.
3 changes: 2 additions & 1 deletion src/common/passport/jwtStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module.exports = () => {
passport.use(
new JwtStrategy(opts, async (jwtPayload, done) => {
try {
const user = await User.findById(jwtPayload.id);
console.log(jwtPayload);
const user = await User.findById(jwtPayload.userId);
if (user) {
return done(null, user);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/common/utils/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ exports.isLoggedIn = async (req, res, next) => {
} catch (error) {
res.status(error.status).json({
message: error.message,
...(error.expiredAt && { expiredAt: error.expiredAt }),
});
}
};
Expand All @@ -59,6 +58,7 @@ exports.isNotLoggedIn = async (req, res, next) => {
await authenticateJWT(req, res);
res.status(403).json({ message: '이미 둜그인된 μƒνƒœμž…λ‹ˆλ‹€.' });
} catch (error) {
// NOTE : μΈμ¦λ˜μ§€ μ•Šμ€ μ‚¬μš©μžμΈ 경우 톡과
if (error.status === 401) {
next();
} else {
Expand Down
49 changes: 11 additions & 38 deletions src/routes/user/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,8 @@ exports.localLogin = async (req, res, next) => {
const accessToken = generateAccessToken(user);
const refreshToken = generateRefreshToken(user);

// production ν™˜κ²½μ—μ„œλŠ” secure ν•„μš” ν˜Ήμ€ LAX둜 λ³€κ²½, 쿠킀와 토큰 μ£ΌκΈ° λ³€κ²½
res.cookie('accessToken', accessToken, {
httpOnly: false,
maxAge: 10 * 60 * 1000,
sameSite: 'None',
secure: true,
});
res.cookie('refreshToken', refreshToken, {
httpOnly: true,
maxAge: 24 * 60 * 60 * 1000,
sameSite: 'None',
secure: true,
});
res.cookie('accessToken', accessToken, config.cookieInAccessTokenOptions);
res.cookie('refreshToken', refreshToken, config.cookieInRefreshTokenOptions);

return sendResponse.ok(res, {
message: SucesssMessage.LOGIN_SUCCESSS,
Expand Down Expand Up @@ -142,18 +131,8 @@ exports.kakaoLogin = async (req, res) => {

const accessToken = generateAccessToken(user);
const refreshToken = generateRefreshToken(user);
res.cookie('accessToken', accessToken, {
httpOnly: false,
maxAge: 10 * 60 * 1000,
sameSite: 'None',
secure: true,
});
res.cookie('refreshToken', refreshToken, {
httpOnly: true,
maxAge: 24 * 60 * 60 * 1000,
sameSite: 'None',
secure: true,
});
res.cookie('accessToken', accessToken, config.cookieInAccessTokenOptions);
res.cookie('refreshToken', refreshToken, config.cookieInRefreshTokenOptions);

sendResponse.ok(res, {
message: SucesssMessage.LOGIN_SUCCESSS,
Expand All @@ -174,23 +153,16 @@ exports.refreshToken = async (req, res) => {
}

jwt.verify(refreshToken, config.jwtRefreshSecret, (err, user) => {
if (err) return res.sendStatus(403);
if (err)
return sendResponse.forbidden(res, {
message: ErrorMessage.REFRESH_TOKEN_ERROR,
});

const newAccessToken = generateAccessToken({ _id: user.userId, nickname: user.nickname, email: user.email });
const newRefreshToken = generateRefreshToken({ _id: user.userId, nickname: user.nickname, email: user.email });

res.cookie('accessToken', newAccessToken, {
httpOnly: false,
maxAge: 10 * 60 * 1000,
sameSite: 'None',
secure: true,
});
res.cookie('refreshToken', newRefreshToken, {
httpOnly: true,
maxAge: 24 * 60 * 60 * 1000,
sameSite: 'None',
secure: true,
});
res.cookie('accessToken', newAccessToken, config.cookieInAccessTokenOptions);
res.cookie('refreshToken', newRefreshToken, config.cookieInRefreshTokenOptions);

sendResponse.ok(res, {
message: SucesssMessage.REFRESH_TOKEN,
Expand All @@ -210,6 +182,7 @@ exports.getProfile = (req, res) => {
};

exports.logout = (_, res) => {
res.cookie('accessToken', '', { httpOnly: true, maxAge: 0 });
res.cookie('refreshToken', '', { httpOnly: true, maxAge: 0 });
return sendResponse.ok(res, {
message: SucesssMessage.LOGOUT_SUCCESS,
Expand Down
4 changes: 2 additions & 2 deletions src/routes/user/user.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ userRouter.get('/check/email', isEmailExist);
// 둜그인
userRouter.post('/local/login', isNotLoggedIn, localLogin);
userRouter.post('/kakao/login', isNotLoggedIn, kakaoLogin);
userRouter.post('/refresh', isNotLoggedIn, refreshToken);
userRouter.post('/logout', isNotLoggedIn, logout);
userRouter.post('/refresh', refreshToken);
userRouter.post('/logout', logout);

userRouter.get('/profile', isLoggedIn, getProfile);

Expand Down

0 comments on commit aecb42d

Please sign in to comment.