mirror of
				https://gitlab.com/comunic/comunicapiv2
				synced 2025-11-04 03:24:04 +00:00 
			
		
		
		
	Can create a new account
This commit is contained in:
		@@ -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
 | 
				
			||||||
	 * 
 | 
						 * 
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										12
									
								
								src/entities/NewAccount.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/entities/NewAccount.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
				
			|||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * New account information
 | 
				
			||||||
 | 
					 * 
 | 
				
			||||||
 | 
					 * @author Pierre HUBERT
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export interface NewAccount {
 | 
				
			||||||
 | 
						firstName: string,
 | 
				
			||||||
 | 
						lastName: string,
 | 
				
			||||||
 | 
						email: string,
 | 
				
			||||||
 | 
						password: string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -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
 | 
				
			||||||
	 * 
 | 
						 * 
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user