1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2025-06-20 16:45:16 +00:00

Sign in user

This commit is contained in:
2019-11-23 13:16:28 +01:00
parent 89e612900c
commit 293752b0f4
7 changed files with 234 additions and 8 deletions

View File

@ -1,6 +1,7 @@
import { Response, Request } from "express";
import { APIHelper } from "../helpers/APIHelper";
import { APIClient } from "./APIClient";
import { checkMail } from "../utils/StringUtils";
/**
* Response to a request
@ -31,7 +32,7 @@ export class RequestHandler {
* @param required If set to true (true by default), an error will
* be thrown if the string is not included in the request
*/
public getString(name : string, minLength : number = 1, required : boolean = true) : string {
public postString(name : string, minLength : number = 1, required : boolean = true) : string {
const param = this.getPostParam(name);
// Check if parameter was not found
@ -47,6 +48,20 @@ export class RequestHandler {
return param;
}
/**
* Get an email address included in a post request
*
* @param name The name of the POST filed
*/
public postEmail(name: string) : string {
const email = this.postString(name, 3);
if(!checkMail(email))
this.error(400, email + " is not a valid email address!");
return email;
}
/**
* Validate API tokens
@ -56,8 +71,8 @@ export class RequestHandler {
public async checkAPITokens() {
// Extract API name & token from request
const apiName = this.getString("serviceName");
const apiToken = this.getString("serviceToken");
const apiName = this.postString("serviceName");
const apiToken = this.postString("serviceToken");
// Validate the client
const client = await APIHelper.GetClient(apiName, apiToken);
@ -81,7 +96,18 @@ export class RequestHandler {
}
/**
* Output an error code
* Get information about API client
*/
public getClientInfo() : APIClient {
if(!this.client)
throw Error("Try to get information about client but client has not been authenticated!");
return this.client;
}
/**
* Output an error code and throws an error
*
* @param code HTTP Status code
* @param message The message to send

View File

@ -0,0 +1,12 @@
/**
* User tokens
*
* @author Pierre HUBERT
*/
export interface UserLoginTokens {
userID : number,
clientID : number,
token1: string,
token2: string,
}