From 143755e5e8a53eeee9f5b2fa7e3b76866c9673d7 Mon Sep 17 00:00:00 2001 From: Mateja Date: Thu, 6 May 2021 22:13:15 +0200 Subject: Adding new users without validation. Also set input type to password in `login.handlebars`. --- controllers/user.js | 32 ++++++++++++++++++++++++++++---- views/login.handlebars | 5 ++++- views/register.handlebars | 3 +++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/controllers/user.js b/controllers/user.js index e25abf8..54b8161 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -1,8 +1,24 @@ +const User = require('../models/user'); + module.exports = { register(req, res) { - console.log(req.body); - res.redirect('/register'); + if (req.body.password === req.body.confirmPassword) { + const newUser = new User({ + firstname: req.body.firstname, + lastname: req.body.lastname, + email: req.body.email, + password: req.body.password + }); + newUser.save().then(() => { + req.flash('msg', 'You successfully registered.'); + res.redirect('/login'); + }); + } + else { + req.flash('msg', 'Validation errors...'); + res.redirect('/register'); + } }, login(req, res) { @@ -15,11 +31,19 @@ module.exports = { }, registerView(req, res) { - res.render('register', {title: 'Register', register: true}); + res.render('register', { + title: 'Register', + register: true, + msg: req.flash('msg') + }); }, loginView(req, res) { - res.render('login', {title: 'Login', login: true}); + res.render('login', { + title: 'Login', + login: true, + msg: req.flash('msg') + }); } }; diff --git a/views/login.handlebars b/views/login.handlebars index 47e2166..fd87ba7 100644 --- a/views/login.handlebars +++ b/views/login.handlebars @@ -1,5 +1,8 @@

{{ title }}

+ {{#if msg}} + + {{/if}}
@@ -7,7 +10,7 @@
- +
diff --git a/views/register.handlebars b/views/register.handlebars index d18a6b8..a2c9f4d 100644 --- a/views/register.handlebars +++ b/views/register.handlebars @@ -1,5 +1,8 @@

{{ title }}

+ {{#if msg}} + + {{/if}}
-- cgit v1.2.3