From 4291d5a85619e85ff4943e0f7f38e43dba7b6260 Mon Sep 17 00:00:00 2001 From: NotReal <117504040+NotReal003@users.noreply.github.com> Date: Mon, 18 Nov 2024 14:48:46 +0530 Subject: [PATCH 1/3] Update index.js --- api/src/routes/auth/index.js | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/api/src/routes/auth/index.js b/api/src/routes/auth/index.js index 7d5f35d..58edce3 100644 --- a/api/src/routes/auth/index.js +++ b/api/src/routes/auth/index.js @@ -107,8 +107,38 @@ router.get('/callback', async (req, res) => { .redirect(DASHBOARD_URL); }); -router.get('/signout', (req, res) => { - res.clearCookie('token').sendStatus(200); +router.get('/signout', async (req, res) => { + const token = req.cookies.token; + + // Clear cookie regardless of token presence + if (!token) { + res.clearCookie('token', { httpOnly: true, secure: true }); + return res.status(200).json({ message: 'Successfully logged out. No active session found.' }); + } + + try { + // Verify the token + const savedToken = await Blacklist.findOne({ blacklistToken: token }); + if (savedToken) { + res.clearCookie('token', { httpOnly: true, secure: true }); + return res.status(200).json({ message: 'Successfully logged out. Active session found and already blocked.' }); + } + const decodedToken = jwt.verify(token, process.env.JWT_SECRET); + + // Add token to blacklist + await Blacklist.create({ blacklistToken: token, user_id: decodedToken.id }); + + res.clearCookie('token', { httpOnly: true, secure: true }); + return res.status(200).json({ message: 'Successfully logged out.' }); + } catch (err) { + // Handle invalid token or verification errors + if (err) { + res.clearCookie('token', { httpOnly: true, secure: true }); + return res.status(200).json({ message: 'Successfully logged out. Invalid or expired session token.' }); + } + + return res.status(500).json({ message: 'Error during logout. Please try again later.' }); + } }); module.exports = router; From 5e20517402de4c228c13f4d084d09603168b9e11 Mon Sep 17 00:00:00 2001 From: NotReal <117504040+NotReal003@users.noreply.github.com> Date: Mon, 18 Nov 2024 14:50:33 +0530 Subject: [PATCH 2/3] Create Blacklist.js --- api/src/models/Blacklist.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 api/src/models/Blacklist.js diff --git a/api/src/models/Blacklist.js b/api/src/models/Blacklist.js new file mode 100644 index 0000000..a7d4c7f --- /dev/null +++ b/api/src/models/Blacklist.js @@ -0,0 +1,9 @@ +const mongoose = require('mongoose'); + +const blacklistSchema = new mongoose.Schema({ + user_id: { type: String }, + blacklistToken: { type: String }, + time: { type: Date, default: Date.now }, +}); + +module.exports = mongoose.model('Blacklist', blacklistSchema); From 475f5914b3824f6d19f2576dd97c91e5f4db1ec4 Mon Sep 17 00:00:00 2001 From: NotReal <117504040+NotReal003@users.noreply.github.com> Date: Mon, 18 Nov 2024 14:51:41 +0530 Subject: [PATCH 3/3] Update index.js --- api/src/routes/auth/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/api/src/routes/auth/index.js b/api/src/routes/auth/index.js index 58edce3..b8b63bb 100644 --- a/api/src/routes/auth/index.js +++ b/api/src/routes/auth/index.js @@ -1,5 +1,6 @@ const express = require('express'); const User = require('../../models/User'); +const Blacklist = require('../../models/Blacklist'); const jwt = require('jsonwebtoken'); const router = express.Router();