Skip to content

Commit

Permalink
fix: only disconnect the current session on logout
Browse files Browse the repository at this point in the history
Other sessions of the same user are left alive.
  • Loading branch information
darrachequesne committed Jan 12, 2024
1 parent 32154cf commit b997b82
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
11 changes: 8 additions & 3 deletions server/src/auth/logout.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
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) => {
if (err) {
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();
});
Expand Down
9 changes: 8 additions & 1 deletion server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -88,6 +94,7 @@ function initEventHandlers({ io, db, config }) {
});

socket.join(userRoom(socket.userId));
socket.join(sessionRoom(socket.request.session.id));

next();
});
Expand Down
4 changes: 4 additions & 0 deletions server/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}

0 comments on commit b997b82

Please sign in to comment.