From b997b827d337c26953ae195682df9ebeb6e9a9e0 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Fri, 12 Jan 2024 12:08:46 +0100 Subject: [PATCH] fix: only disconnect the current session on logout Other sessions of the same user are left alive. --- server/src/auth/logout.js | 11 ++++++++--- server/src/index.js | 9 ++++++++- server/src/util.js | 4 ++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/server/src/auth/logout.js b/server/src/auth/logout.js index 31afe83..0315bbd 100644 --- a/server/src/auth/logout.js +++ b/server/src/auth/logout.js @@ -1,7 +1,8 @@ -import { logger, userRoom } from "../util.js"; +import { logger, sessionRoom } from "../util.js"; export function logout({ app, io }) { app.post("/logout", (req, res, next) => { + const sessionId = req.session.id; const userId = req.user.id; req.logout((err) => { @@ -9,9 +10,13 @@ export function logout({ app, io }) { return next(err); } - logger.info("user [%s] has logged out", userId); + logger.info( + "user [%s] has logged out from session [%s]", + userId, + sessionId, + ); - io.in(userRoom(userId)).disconnectSockets(); + io.in(sessionRoom(sessionId)).disconnectSockets(); res.status(204).end(); }); diff --git a/server/src/index.js b/server/src/index.js index b18f809..13c5559 100644 --- a/server/src/index.js +++ b/server/src/index.js @@ -18,7 +18,13 @@ import { getUser } from "./user/get.js"; import { initAuth } from "./auth/index.js"; import { reachUser } from "./user/reach.js"; import { searchUsers } from "./user/search.js"; -import { channelRoom, userRoom, userStateRoom, logger } from "./util.js"; +import { + channelRoom, + userRoom, + userStateRoom, + logger, + sessionRoom, +} from "./util.js"; const CLEANUP_ZOMBIE_USERS_INTERVAL_IN_MS = 60_000; @@ -88,6 +94,7 @@ function initEventHandlers({ io, db, config }) { }); socket.join(userRoom(socket.userId)); + socket.join(sessionRoom(socket.request.session.id)); next(); }); diff --git a/server/src/util.js b/server/src/util.js index e3a47d0..34db3b5 100644 --- a/server/src/util.js +++ b/server/src/util.js @@ -42,6 +42,10 @@ export function userRoom(userId) { return `user:${userId}`; } +export function sessionRoom(sessionId) { + return `session:${sessionId}`; +} + export function userStateRoom(userId) { return `user_state:${userId}`; }