From 6d225354ced2e36f69936df09e8af5d566be3c8f Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 1 May 2020 09:27:03 +0200 Subject: [PATCH] Can disconnect user from all its devices --- src/controllers/AccountController.ts | 12 ++++++++++++ src/controllers/Routes.ts | 2 ++ src/helpers/AccountHelper.ts | 5 +++++ 3 files changed, 19 insertions(+) diff --git a/src/controllers/AccountController.ts b/src/controllers/AccountController.ts index 505dff2..f250d83 100644 --- a/src/controllers/AccountController.ts +++ b/src/controllers/AccountController.ts @@ -102,6 +102,18 @@ export class AccountController { handler.success("User has been disconnected!"); } + /** + * Disconnect current user from all its connected devices + * (destroy all its login tokens) + * + * @param h Request handler + */ + public static async DisconnectAllDevices(h: RequestHandler) { + await AccountHelper.DeleteAllUserLoginTokens(h.getUserId()); + + h.success(); + } + /** * Get current user ID * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index f0d40f5..a419f75 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -66,6 +66,8 @@ export const Routes : Route[] = [ {path: "/account/logout", cb: (h) => AccountController.LogoutUser(h)}, {path: "/user/disconnectUSER", cb: (h) => AccountController.LogoutUser(h)}, // Legacy + + {path: "/account/disconnect_all_devices", cb: (h) => AccountController.DisconnectAllDevices(h)}, {path: "/account/id", cb: (h) => AccountController.CurrentUserID(h)}, {path: "/user/getCurrentUserID", cb: (h) => AccountController.CurrentUserID(h)}, // Legacy diff --git a/src/helpers/AccountHelper.ts b/src/helpers/AccountHelper.ts index d704839..c77030f 100644 --- a/src/helpers/AccountHelper.ts +++ b/src/helpers/AccountHelper.ts @@ -317,6 +317,11 @@ export class AccountHelper { * @param userID Target user ID */ public static async DeleteAllUserLoginTokens(userID: number) { + + // First, close all sockets of user + await UserWebSocketController.CloseAllUserSockets(userID); + + // Destroy tokens in the database await DatabaseHelper.DeleteRows(USERS_TOKENS_TABLE, { user_id: userID })