Skip to content

Commit

Permalink
refactor: 로그아웃 로직 레이어 분리 Murakano#89
Browse files Browse the repository at this point in the history
  • Loading branch information
jjikky committed Oct 23, 2024
1 parent b95191a commit 9e937b8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
43 changes: 16 additions & 27 deletions src/routes/user/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const {
loginBodySchema,
requestBodySchema,
} = require('./user.schema');
const { generateAccessToken, generateRefreshToken } = require('../../common/utils/auth');
const { catchAsync } = require('../../common/utils/catch-async');

exports.register = catchAsync(async (req, res) => {
Expand Down Expand Up @@ -132,15 +131,9 @@ exports.getProfile = (req, res) => {
exports.logout = async (req, res) => {
const refreshToken = req.cookies.refreshToken;
if (refreshToken) {
try {
const email = req.user.email;
await redisClient.del(email);
} catch (err) {
console.error('Redis error:', err);
}
await userService.logout(req.user.email);
}

res.clearCookie('refreshToken', config.cookieInRefreshTokenDeleteOptions);
clearRefreshTokenCookie(res);
return sendResponse.ok(res, {
message: SuccessMessage.LOGOUT_SUCCESS,
});
Expand All @@ -164,24 +157,16 @@ exports.delRecentSearch = catchAsync(async (req, res) => {
});
}, ErrorMessage.DELETE_RECENT_WORD_ERROR);

exports.postWords = async (req, res) => {
try {
const validData = validateRequest(requestBodySchema, req.body);
const { _id } = req.user;
const { formData, type, nickname } = validData;
const result = await userService.postWords(_id, formData, nickname, type);
sendResponse.ok(res, {
message: SuccessMessage.REGISTER_WORDS_SUCCESS,
data: result,
});
} catch (error) {
console.log(error);
if (error?.type === 'ajv') {
return sendResponse.badRequest(res, ErrorMessage.ADD_REQUEST_WORDS_ERROR);
}
sendResponse.fail(req, res, ErrorMessage.REQUEST_DUPLICATE_ERROR);
}
};
exports.postWords = catchAsync(async (req, res) => {
const validData = validateRequest(requestBodySchema, req.body);
const { _id } = req.user;
const { formData, type, nickname } = validData;
const result = await userService.postWords(_id, formData, nickname, type);
sendResponse.ok(res, {
message: SuccessMessage.REGISTER_WORDS_SUCCESS,
data: result,
});
}, ErrorMessage.REQUEST_DUPLICATE_ERROR);

exports.UserRequests = catchAsync(async (req, res) => {
const { _id } = req.user;
Expand Down Expand Up @@ -261,3 +246,7 @@ exports.deleteUser = catchAsync(async (req, res) => {
const setRefreshTokenCookie = (res, refreshToken) => {
res.cookie('refreshToken', refreshToken, config.cookieInRefreshTokenOptions);
};

const clearRefreshTokenCookie = (res) => {
res.clearCookie('refreshToken', config.cookieInRefreshTokenDeleteOptions);
};
4 changes: 4 additions & 0 deletions src/routes/user/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ exports.isUserExist = async (userId) => {
return await userRepository.findUserById(userId);
};

exports.logout = async (email) => {
await redisClient.del(email);
};

exports.getRecentSearches = async (userId) => {
const recentSearches = await userRepository.getRecentSearches(userId);
return recentSearches;
Expand Down

0 comments on commit 9e937b8

Please sign in to comment.