Fix logout

This commit is contained in:
Louis Lam 2023-12-09 17:17:24 +08:00
parent 16a5f2a175
commit fd7f539089
3 changed files with 16 additions and 3 deletions

View file

@ -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) {

View file

@ -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<User | null> {

View file

@ -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) {