1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-25 23:09:22 +00:00

Can create a new account

This commit is contained in:
Pierre HUBERT 2019-12-30 13:44:54 +01:00
parent 48cb254b9b
commit 994d1540dc
5 changed files with 76 additions and 1 deletions

View File

@ -1,6 +1,8 @@
import { RequestHandler } from "../entities/RequestHandler"; import { RequestHandler } from "../entities/RequestHandler";
import { AccountHelper } from "../helpers/AccountHelper"; import { AccountHelper } from "../helpers/AccountHelper";
import { UserHelper } from "../helpers/UserHelper"; import { UserHelper } from "../helpers/UserHelper";
import { NewAccount } from "../entities/NewAccount";
import { removeHTMLNodes } from "../utils/StringUtils";
/** /**
* Account controller * Account controller
@ -10,6 +12,35 @@ import { UserHelper } from "../helpers/UserHelper";
export class AccountController { export class AccountController {
/**
* Create a new account
*
* @param h Request handler
*/
public static async Create(h: RequestHandler) {
// TODO : add API limit
// Get & check email address
const email = h.postEmail("emailAddress");
if(await AccountHelper.ExistsEmail(email))
h.error(409, "This email address already belongs to an account!");
const newAccount = <NewAccount>{
firstName: removeHTMLNodes(h.postString("firstName")),
lastName: removeHTMLNodes(h.postString("lastName")),
email: email,
password: h.postString("password", 3)
};
// Try to create the account
await AccountHelper.Create(newAccount);
// TODO : trigger the API limit
// Success
h.success("The account has been created!");
}
/** /**
* Attempt to login user * Attempt to login user
* *

View File

@ -34,6 +34,8 @@ export const Routes : Route[] = [
{type: RouteType.GET, path: "/", cb: WelcomeController.HomeMessage, needLogin: false}, {type: RouteType.GET, path: "/", cb: WelcomeController.HomeMessage, needLogin: false},
// Account controller // Account controller
{path: "/account/create", cb: (h) => AccountController.Create(h), needLogin: false},
{path: "/account/login", cb: (h) => AccountController.LoginUser(h), needLogin: false}, {path: "/account/login", cb: (h) => AccountController.LoginUser(h), needLogin: false},
{path: "/user/connectUSER", cb: (h) => AccountController.LoginUser(h), needLogin: false}, // Legacy {path: "/user/connectUSER", cb: (h) => AccountController.LoginUser(h), needLogin: false}, // Legacy

View File

@ -0,0 +1,12 @@
/**
* New account information
*
* @author Pierre HUBERT
*/
export interface NewAccount {
firstName: string,
lastName: string,
email: string,
password: string
}

View File

@ -3,7 +3,8 @@ import { APIClient } from "../entities/APIClient";
import { UserLoginTokens } from "../entities/UserLoginTokens"; import { UserLoginTokens } from "../entities/UserLoginTokens";
import { DatabaseHelper } from "./DatabaseHelper"; import { DatabaseHelper } from "./DatabaseHelper";
import { UserHelper } from "./UserHelper"; import { UserHelper } from "./UserHelper";
import { time } from "../utils/DateUtils"; import { time, mysql_date } from "../utils/DateUtils";
import { NewAccount } from "../entities/NewAccount";
/** /**
* Account helper * Account helper
@ -16,6 +17,23 @@ const USERS_TOKENS_TABLE = "comunic_api_users_tokens";
export class AccountHelper { export class AccountHelper {
/**
* Create a new account
*
* @param info Information about the new account
*/
public static async Create(info: NewAccount) {
const data = {
nom: info.lastName,
prenom: info.firstName,
date_creation: mysql_date(),
mail: info.email,
password: this.CryptPassword(info.password)
};
await DatabaseHelper.InsertRow(USER_TABLE, data);
}
/** /**
* Given email address and password, try to sign in user * Given email address and password, try to sign in user
* *

View File

@ -7,3 +7,15 @@
export function time() : number { export function time() : number {
return Math.floor((new Date()).getTime()/1000); return Math.floor((new Date()).getTime()/1000);
} }
/**
* Get the current date formatted for the "DATETIME" object of
* a MySQL database
*/
export function mysql_date() : string {
const date = new Date();
return date.getFullYear() + "-" + (date.getMonth()+1) + "-" + (date.getDate() + 1)
+ " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
}