1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 13:29:22 +00:00

Can sign out user

This commit is contained in:
Pierre HUBERT 2019-11-23 14:03:14 +01:00
parent 6119d5d0ff
commit e635802296
4 changed files with 63 additions and 3 deletions

View File

@ -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
*

View File

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

View File

@ -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
*

View File

@ -122,12 +122,43 @@ export class DatabaseHelper {
*/
static async InsertRow(table : string, values : any) : Promise<number> {
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();
});
})
}
}