1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2025-06-21 00:55:17 +00:00

Can check user tokens

This commit is contained in:
2019-11-23 13:47:06 +01:00
parent 1ddf156cc4
commit c304c2f88e
4 changed files with 70 additions and 3 deletions

View File

@ -2,6 +2,7 @@ import { Response, Request } from "express";
import { APIHelper } from "../helpers/APIHelper";
import { APIClient } from "./APIClient";
import { checkMail } from "../utils/StringUtils";
import { AccountHelper } from "../helpers/AccountHelper";
/**
* Response to a request
@ -12,6 +13,7 @@ import { checkMail } from "../utils/StringUtils";
export class RequestHandler {
private client : APIClient = null;
private userID : number = -1;
private responseSent = false;
@ -97,6 +99,30 @@ export class RequestHandler {
this.client = client;
}
/**
* Validate user tokens
*
* @param required Specify whether the user MUST be authenticated or not
*/
public async checkUserTokens(required ?: boolean) {
const token1 = this.postString("userToken1", 0, false);
const token2 = this.postString("userToken2", 0, false);
if(token1.length < 1 || token2.length < 1) {
if(required !== false)
this.error(401, "This method requires the user to be signed in!");
return;
}
// Validate user tokens
this.userID = await AccountHelper.GetUserIdFromTokens(this.getClientInfo(), token1, token2);
if(this.userID < 1)
this.error(412, "Please check your login tokens!");
}
/**
* Get information about API client
*/
@ -108,6 +134,16 @@ export class RequestHandler {
return this.client;
}
/**
* Get information about current user
*/
public getUserId() : number {
if(this.userID < 1)
throw Error("Trying to get user ID but none are available!");
return this.userID;
}
/**
* Output an error code and throws an error
*