From 401eaa56e1bfc3e7a7d8c5be26afa86efa23c327 Mon Sep 17 00:00:00 2001 From: matt Date: Sun, 13 Feb 2022 22:11:15 +0000 Subject: [PATCH] Added route for registering an Account --- lib/User.js | 7 ++++++- routes/main.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/User.js b/lib/User.js index 8552dca..f723e72 100644 --- a/lib/User.js +++ b/lib/User.js @@ -61,7 +61,12 @@ class User { } - login() { + login(req) { + req.session.authenticated = true; + req.session.userId = this.id; + req.session.userType = this.type; + req.session.fullName = `${this.firstName} ${this.lastName}`; + } static async hashPassword(password) { return await bcrypt.hash(password, 10); diff --git a/routes/main.js b/routes/main.js index 160c24f..71b2d64 100644 --- a/routes/main.js +++ b/routes/main.js @@ -3,6 +3,9 @@ const express = require('express'); const router = express.Router(); +const Account = require('../lib/Account'); +const validator = require('../lib/validator'); + router.get('/', (req, res) => { return res.render('index', { title: 'Stratos - Home' @@ -34,6 +37,49 @@ router.get('/logout', (req, res) => { }); }); +router.post('/register', async (req, res) => { + let fields; + try { + fields = validator.validate(req.body, + [ + 'fname', + 'lname', + 'email', + 'password', + 'confPassword' + ], + { + email: 'email', + password: [ 'password', 'confPassword' ] + } + ).fields; + } catch (e) { + console.error(e); + return res.status(400).json({ status: 'Invalid' }); + } + + let a; + try { + a = await Account.createAccount( + fields.get('fname'), + fields.get('onames'), + fields.get('lname'), + fields.get('email'), + fields.get('password') + ); + } catch (e) { + console.error(e); + return res.render('error', { + code: 400, + msg: 'Unable to create account' + }); + } + + a.login(req); + + return res.redirect('/login'); +}); + module.exports = { root: '/', router: router