mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-01 22:19:26 +00:00
Added route for registering an Account
This commit is contained in:
@@ -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) {
|
static async hashPassword(password) {
|
||||||
return await bcrypt.hash(password, 10);
|
return await bcrypt.hash(password, 10);
|
||||||
|
|||||||
@@ -3,6 +3,9 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
|
const Account = require('../lib/Account');
|
||||||
|
const validator = require('../lib/validator');
|
||||||
|
|
||||||
router.get('/', (req, res) => {
|
router.get('/', (req, res) => {
|
||||||
return res.render('index', {
|
return res.render('index', {
|
||||||
title: 'Stratos - Home'
|
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 = {
|
module.exports = {
|
||||||
root: '/',
|
root: '/',
|
||||||
router: router
|
router: router
|
||||||
|
|||||||
Reference in New Issue
Block a user