From 9e937b89bb592a2db44b86f5bb4dbdfcb7cf6d93 Mon Sep 17 00:00:00 2001 From: jjikky Date: Thu, 24 Oct 2024 02:26:55 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EB=A1=9C=EA=B7=B8=EC=95=84?= =?UTF-8?q?=EC=9B=83=20=EB=A1=9C=EC=A7=81=20=EB=A0=88=EC=9D=B4=EC=96=B4=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC=20#89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/user/user.controller.js | 43 +++++++++++------------------- src/routes/user/user.service.js | 4 +++ 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/routes/user/user.controller.js b/src/routes/user/user.controller.js index 0646ab9..dc88e4e 100755 --- a/src/routes/user/user.controller.js +++ b/src/routes/user/user.controller.js @@ -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) => { @@ -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, }); @@ -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; @@ -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); +}; diff --git a/src/routes/user/user.service.js b/src/routes/user/user.service.js index 4988440..0e030a3 100644 --- a/src/routes/user/user.service.js +++ b/src/routes/user/user.service.js @@ -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;