From 321a2566de3d1718b9839b8f0220b129ae501d7d Mon Sep 17 00:00:00 2001 From: LingandRX Date: Wed, 1 Jan 2025 17:34:14 +0800 Subject: [PATCH] =?UTF-8?q?feat(user):=20=E6=B7=BB=E5=8A=A0=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E7=99=BB=E5=87=BA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 userController.js 中实现 logout 方法,销毁用户会话 - 在 routes/index.js 中添加 logout 路由 --- controllers/userController.js | 14 ++++++++++++++ routes/index.js | 2 ++ 2 files changed, 16 insertions(+) diff --git a/controllers/userController.js b/controllers/userController.js index fabbf2e..28d8026 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -107,6 +107,20 @@ exports.login = [ } ] +exports.logout = async (req, res) => { + try { + req.session.destroy() + return FetchResult.formatResult(res, HTTP_STATUS.OK, messages.user.logout) + } catch (err) { + logger('Error logging out: ', err) + return FetchResult.formatResult( + res, + HTTP_STATUS.INTERNAL_SERVER_ERROR, + 'Internal server error' + ) + } +} + exports.getUserExist = async (req, res) => { try { const { account } = req.query diff --git a/routes/index.js b/routes/index.js index 968ac03..92f258b 100644 --- a/routes/index.js +++ b/routes/index.js @@ -13,4 +13,6 @@ router.get('/', function (req, res) { router.post('/login', userController.login) +router.post('/logout', userController.logout) + module.exports = router