diff --git a/src/controllers/AccountController.ts b/src/controllers/AccountController.ts index 13f22b1..62fca01 100644 --- a/src/controllers/AccountController.ts +++ b/src/controllers/AccountController.ts @@ -1,6 +1,8 @@ import { RequestHandler } from "../entities/RequestHandler"; import { AccountHelper } from "../helpers/AccountHelper"; import { UserHelper } from "../helpers/UserHelper"; +import { NewAccount } from "../entities/NewAccount"; +import { removeHTMLNodes } from "../utils/StringUtils"; /** * Account controller @@ -10,6 +12,35 @@ import { UserHelper } from "../helpers/UserHelper"; 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 = { + 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 * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index e6b3590..93c7db3 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -34,6 +34,8 @@ export const Routes : Route[] = [ {type: RouteType.GET, path: "/", cb: WelcomeController.HomeMessage, needLogin: false}, // Account controller + {path: "/account/create", cb: (h) => AccountController.Create(h), needLogin: false}, + {path: "/account/login", cb: (h) => AccountController.LoginUser(h), needLogin: false}, {path: "/user/connectUSER", cb: (h) => AccountController.LoginUser(h), needLogin: false}, // Legacy diff --git a/src/entities/NewAccount.ts b/src/entities/NewAccount.ts new file mode 100644 index 0000000..44b2f16 --- /dev/null +++ b/src/entities/NewAccount.ts @@ -0,0 +1,12 @@ +/** + * New account information + * + * @author Pierre HUBERT + */ + +export interface NewAccount { + firstName: string, + lastName: string, + email: string, + password: string +} \ No newline at end of file diff --git a/src/helpers/AccountHelper.ts b/src/helpers/AccountHelper.ts index 6eff1f0..7639859 100644 --- a/src/helpers/AccountHelper.ts +++ b/src/helpers/AccountHelper.ts @@ -3,7 +3,8 @@ import { APIClient } from "../entities/APIClient"; import { UserLoginTokens } from "../entities/UserLoginTokens"; import { DatabaseHelper } from "./DatabaseHelper"; import { UserHelper } from "./UserHelper"; -import { time } from "../utils/DateUtils"; +import { time, mysql_date } from "../utils/DateUtils"; +import { NewAccount } from "../entities/NewAccount"; /** * Account helper @@ -16,6 +17,23 @@ const USERS_TOKENS_TABLE = "comunic_api_users_tokens"; 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 * diff --git a/src/utils/DateUtils.ts b/src/utils/DateUtils.ts index a12e73b..ad138da 100644 --- a/src/utils/DateUtils.ts +++ b/src/utils/DateUtils.ts @@ -6,4 +6,16 @@ export function time() : number { 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(); } \ No newline at end of file