Skip to content

Node.js based forum software built for the modern web

fix: log out user if session cookie resolves to non-existent uid

Julian Lam5d9da6035e3a
diff --git a/src/controllers/authentication.js b/src/controllers/authentication.js
index 581f7d931c..162bea8ecd 100644
--- a/src/controllers/authentication.js
+++ b/src/controllers/authentication.js
@@ -490,7 +490,7 @@ authenticationController.logout = async function (req, res) {
};
await plugins.hooks.fire('filter:user.logout', payload);
- if (req.body?.noscript === 'true') {
+ if (req.body?.noscript === 'true' || res.locals.logoutRedirect === true) {
return res.redirect(payload.next);
}
res.status(200).send(payload);
diff --git a/src/middleware/user.js b/src/middleware/user.js
index c49481e625..620c4bad9c 100644
--- a/src/middleware/user.js
+++ b/src/middleware/user.js
@@ -53,6 +53,12 @@ module.exports = function (middleware) {
}
if (req.loggedIn) {
+ const exists = await user.exists(req.uid);
+ if (!exists) {
+ res.locals.logoutRedirect = true;
+ return controllers.authentication.logout(req, res);
+ }
+
return true;
} else if (req.headers.hasOwnProperty('authorization')) {
const user = await passportAuthenticateAsync(req, res);