lighter lighter - 25 days ago 16
Node.js Question

NodeJS express-validator with passport

I write a signup form table, and I use

passport-local
is work, but I want add
express-validator
to validate my form data. I add validation on the router, this is my
router/index.js
code:

/* Handle Registration POST */
router.post('/signup', function(req, res) {
req.assert('email', 'A valid email is required').isEmail();
var errors = req.validationErrors();

if(errors){ //No errors were found. Passed Validation!
res.render('register', {
message: 'Mail type fault',
errors: errors
});
}
else
{
passport.authenticate('signup', {
successRedirect: '/home',
failureRedirect: '/signup',
failureFlash : true
});
}
});


The validation is work, but if successful and then the web page will loading for a long time and no respond. I have search the passport doc, but no idea to fix it.

This is origin code, it's work

/* Handle Registration POST */
router.post('/signup', passport.authenticate('signup', {
successRedirect: '/home',
failureRedirect: '/signup',
failureFlash : true
}));


I think I can use jquery to check out, but I don't do it. Because I just want to try use validator with passport.

Answer

Doing the validation in the LocalStrategy rather than the router should work just fine:

passport.use('signup', new LocalStrategy({
    passReqToCallback: true
}, function(req, username, password, callback) {
    /* do your stuff here */
    /* req.assert(...) */
}));
Comments