From 119445fd6ecfb58ddbf2c77c8ca798bc2442442e Mon Sep 17 00:00:00 2001 From: matt Date: Sun, 13 Feb 2022 22:07:53 +0000 Subject: [PATCH] Implemented User.creareUser --- lib/User.js | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/User.js b/lib/User.js index 6077f0d..8552dca 100644 --- a/lib/User.js +++ b/lib/User.js @@ -1,5 +1,8 @@ 'use strict'; +const bcrypt = require('bcrypt'); +const crypto = require('crypto'); + const DatabaseConnectionPool = require('./DatabaseConnectionPool'); class User { @@ -60,14 +63,47 @@ class User { login() { + static async hashPassword(password) { + return await bcrypt.hash(password, 10); } - static hashPassword(password) { + static async createUser(type, fname, oname, lname, email, password) { + const conn = await new DatabaseConnectionPool(); - } + const uuid = crypto.randomUUID(); + const hashedPassword = await User.hashPassword(password); - static createUser() { + const sql = ` + insert into ${type} ( + ${type}Id, + email, + firstName, + otherNames, + lastName, + password) + VALUES (?, ?, ?, ?, ?, ?); + `; + await conn.runQuery(sql, [ + uuid, + email, + fname, + oname, + lname, + hashedPassword + ]); + + let res; + switch (type) { + case 'account': + res = new (require('./Account'))(uuid); + break; + default: + throw new Error( + `Cannot create user of type ${type}`); + } + + return res; } static getUserByEmail() {