Add express validators to signup page, replaces custom written if statements

This commit is contained in:
Sahat Yalkabov
2014-01-23 22:39:59 -05:00
parent f26940942a
commit be73f9c62f
2 changed files with 15 additions and 21 deletions

View File

@ -25,7 +25,7 @@ exports.getSignup = function(req, res) {
if (req.user) return res.redirect('/');
res.render('account/signup', {
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) {
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) {
errors.push('Email cannot be blank.');
}
var errors = req.validationErrors();
if (!req.body.password) {
errors.push('Password cannot be blank.');
}
if (req.body.password !== req.body.confirmPassword) {
errors.push('Passwords do not match.');
}
if (errors.length) {
req.flash('messages', errors);
if (errors) {
req.flash('errors', errors);
return res.redirect('/signup');
}
@ -100,7 +94,7 @@ exports.postSignup = function(req, res, next) {
user.save(function(err) {
if (err) {
if (err.code === 11000) {
req.flash('messages', 'User already exists.');
req.flash('errors', { msg: 'User already exists.' });
}
return res.redirect('/signup');
}

View File

@ -1,10 +1,10 @@
extends ../layout
block content
if messages.length
.alert.alert-danger
for message in messages
div= message
if errors.length
.alert.alert-danger.animated.fadeIn
for error in errors
div= error.msg
form.form-horizontal(id='signup-form', method='POST')
legend Signup
@ -22,6 +22,6 @@ block content
input.form-control(type='password', name='confirmPassword', id='confirmPassword', placeholder='Confirm Password')
.form-group
.col-sm-offset-3.col-sm-7
button.btn.btn-primary(type='submit')
button.btn.btn-success(type='submit')
i.fa.fa-check
| Signup