From e635802296e8ec94185a1289cfd73a636a11bf8a Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 23 Nov 2019 14:03:14 +0100 Subject: [PATCH] Can sign out user --- src/controllers/AccountController.ts | 13 ++++++++++ src/controllers/Routes.ts | 3 +++ src/helpers/AccountHelper.ts | 13 ++++++++++ src/helpers/DatabaseHelper.ts | 37 +++++++++++++++++++++++++--- 4 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/controllers/AccountController.ts b/src/controllers/AccountController.ts index 15ec87c..d67ca83 100644 --- a/src/controllers/AccountController.ts +++ b/src/controllers/AccountController.ts @@ -41,6 +41,19 @@ export class AccountController { }); } + /** + * Disconnect user + * + * @param handler + */ + public static async LogoutUser(handler: RequestHandler) { + + await AccountHelper.DestroyUserTokens(handler.getClientInfo(), + handler.getUserId()); + + handler.success("User has been disconnected!"); + } + /** * Get current user ID * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 03a835c..cd33465 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -28,6 +28,9 @@ export const Routes : Route[] = [ // Account controller {path: "/account/login", cb: AccountController.LoginUser, needLogin: false}, {path: "/user/connectUSER", cb: AccountController.LoginUser, needLogin: false}, // Legacy + + {path: "/account/logout", cb: AccountController.LogoutUser}, + {path: "/user/disconnectUSER", cb: AccountController.LogoutUser}, // Legacy {path: "/account/id", cb: AccountController.CurrentUserID}, {path: "/user/getCurrentUserID", cb: AccountController.CurrentUserID}, // Legacy diff --git a/src/helpers/AccountHelper.ts b/src/helpers/AccountHelper.ts index 736990c..d4dcf3c 100644 --- a/src/helpers/AccountHelper.ts +++ b/src/helpers/AccountHelper.ts @@ -107,6 +107,19 @@ export class AccountHelper { return Number(row.user_id); } + /** + * Destroy user tokens + * + * @param client Information about the client + * @param userID Target user ID + */ + public static async DestroyUserTokens(client: APIClient, userID: number) { + return DatabaseHelper.DeleteRows(USERS_TOKENS_TABLE, { + service_id: client.id, + user_id: userID + }); + } + /** * Crypt a password * diff --git a/src/helpers/DatabaseHelper.ts b/src/helpers/DatabaseHelper.ts index 852badc..a9567e9 100644 --- a/src/helpers/DatabaseHelper.ts +++ b/src/helpers/DatabaseHelper.ts @@ -122,12 +122,43 @@ export class DatabaseHelper { */ static async InsertRow(table : string, values : any) : Promise { return new Promise((resolve, reject) => { - this.connection.query("INSERT INTO " + table + " SET ?", values, (err, results, fields) => { + this.connection.query("INSERT INTO " + table + " SET ?", values, (err, results, f) => { if(err) reject(err); - - resolve(results.insertId); + else + resolve(results.insertId); }); }); } + + /** + * Delete entries from a table + * + * @param table Target table + * @param where Where arguments + */ + static async DeleteRows(table: string, where: any) { + + let whereArgs = ""; + let args = []; + + // Process conditions + for (const key in where) { + if (where.hasOwnProperty(key)) { + const value = where[key]; + whereArgs += (whereArgs == "" ? "" : " AND ") + key + " = ?"; + args.push(value); + } + } + + if(whereArgs == "") + throw Error("Error : table could accidentally get purged!"); + + return new Promise((resolve, reject) => { + this.connection.query("DELETE FROM " + table + " WHERE " + whereArgs, args, (err, r, f) => { + if(err) reject(err) + else resolve(); + }); + }) + } } \ No newline at end of file