Add express validators to signup page, replaces custom written if statements
This commit is contained in:
@ -25,7 +25,7 @@ exports.getSignup = function(req, res) {
|
|||||||
if (req.user) return res.redirect('/');
|
if (req.user) return res.redirect('/');
|
||||||
res.render('account/signup', {
|
res.render('account/signup', {
|
||||||
title: 'Create Account',
|
title: 'Create Account',
|
||||||
messages: req.flash('messages')
|
errors: req.flash('errors')
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -73,22 +73,16 @@ exports.postLogin = function(req, res, next) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
exports.postSignup = function(req, res, next) {
|
exports.postSignup = function(req, res, next) {
|
||||||
var errors = [];
|
req.assert('email', 'Email cannot be blank').notEmpty();
|
||||||
|
req.assert('email', 'Email is not valid').isEmail();
|
||||||
|
req.assert('password', 'Password cannot be blank').notEmpty();
|
||||||
|
req.assert('password', 'Password must be at least 4 characters long').len(4);
|
||||||
|
req.assert('confirmPassword', 'Passwords do not match').equals(req.body.password);
|
||||||
|
|
||||||
if (!req.body.email) {
|
var errors = req.validationErrors();
|
||||||
errors.push('Email cannot be blank.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!req.body.password) {
|
if (errors) {
|
||||||
errors.push('Password cannot be blank.');
|
req.flash('errors', errors);
|
||||||
}
|
|
||||||
|
|
||||||
if (req.body.password !== req.body.confirmPassword) {
|
|
||||||
errors.push('Passwords do not match.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (errors.length) {
|
|
||||||
req.flash('messages', errors);
|
|
||||||
return res.redirect('/signup');
|
return res.redirect('/signup');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,7 +94,7 @@ exports.postSignup = function(req, res, next) {
|
|||||||
user.save(function(err) {
|
user.save(function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
if (err.code === 11000) {
|
if (err.code === 11000) {
|
||||||
req.flash('messages', 'User already exists.');
|
req.flash('errors', { msg: 'User already exists.' });
|
||||||
}
|
}
|
||||||
return res.redirect('/signup');
|
return res.redirect('/signup');
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
extends ../layout
|
extends ../layout
|
||||||
|
|
||||||
block content
|
block content
|
||||||
if messages.length
|
if errors.length
|
||||||
.alert.alert-danger
|
.alert.alert-danger.animated.fadeIn
|
||||||
for message in messages
|
for error in errors
|
||||||
div= message
|
div= error.msg
|
||||||
|
|
||||||
form.form-horizontal(id='signup-form', method='POST')
|
form.form-horizontal(id='signup-form', method='POST')
|
||||||
legend Signup
|
legend Signup
|
||||||
@ -22,6 +22,6 @@ block content
|
|||||||
input.form-control(type='password', name='confirmPassword', id='confirmPassword', placeholder='Confirm Password')
|
input.form-control(type='password', name='confirmPassword', id='confirmPassword', placeholder='Confirm Password')
|
||||||
.form-group
|
.form-group
|
||||||
.col-sm-offset-3.col-sm-7
|
.col-sm-offset-3.col-sm-7
|
||||||
button.btn.btn-primary(type='submit')
|
button.btn.btn-success(type='submit')
|
||||||
i.fa.fa-check
|
i.fa.fa-check
|
||||||
| Signup
|
| Signup
|
||||||
|
Reference in New Issue
Block a user