From fd7f539089838cdd6508f7c69ec39822598c2a32 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Sat, 9 Dec 2023 17:17:24 +0800 Subject: [PATCH] Fix logout --- backend/dockge-server.ts | 2 +- backend/socket-handlers/main-socket-handler.ts | 14 +++++++++++++- extra/reset-password.ts | 3 ++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/backend/dockge-server.ts b/backend/dockge-server.ts index b774bf6..c6c9526 100644 --- a/backend/dockge-server.ts +++ b/backend/dockge-server.ts @@ -610,7 +610,7 @@ export class DockgeServer { * @param {string} userID * @param {string?} currentSocketID */ - disconnectAllSocketClient(userID: number, currentSocketID? : string) { + disconnectAllSocketClients(userID: number, currentSocketID? : string) { for (const rawSocket of this.io.sockets.sockets.values()) { let socket = rawSocket as DockgeSocket; if (socket.userID === userID && socket.id !== currentSocketID) { diff --git a/backend/socket-handlers/main-socket-handler.ts b/backend/socket-handlers/main-socket-handler.ts index e82b072..e6734d5 100644 --- a/backend/socket-handlers/main-socket-handler.ts +++ b/backend/socket-handlers/main-socket-handler.ts @@ -211,7 +211,7 @@ export class MainSocketHandler extends SocketHandler { let user = await doubleCheckPassword(socket, password.currentPassword); await user.resetPassword(password.newPassword); - server.disconnectAllSocketClient(user.id, socket.id); + server.disconnectAllSocketClients(user.id, socket.id); callback({ ok: true, @@ -282,6 +282,18 @@ export class MainSocketHandler extends SocketHandler { } } }); + + // Disconnect all other socket clients of the user + socket.on("disconnectOtherSocketClients", async () => { + try { + checkLogin(socket); + server.disconnectAllSocketClients(socket.userID, socket.id); + } catch (e) { + if (e instanceof Error) { + log.warn("disconnectOtherSocketClients", e.message); + } + } + }); } async login(username : string, password : string) : Promise { diff --git a/extra/reset-password.ts b/extra/reset-password.ts index 3d38fe1..4ba51ee 100644 --- a/extra/reset-password.ts +++ b/extra/reset-password.ts @@ -48,6 +48,8 @@ export const main = async () => { // Reset all sessions by reset jwt secret await server.initJWTSecret(); + console.log("Password reset successfully."); + // Disconnect all other socket clients of the user await disconnectAllSocketClients(user.username, password); @@ -56,7 +58,6 @@ export const main = async () => { console.log("Passwords do not match, please try again."); } } - console.log("Password reset successfully."); } } catch (e) { if (e instanceof Error) {