lighter lighter - 1 year ago 242
Node.js Question

NodeJS express-validator with passport

I write a signup form table, and I use

is work, but I want add
to validate my form data. I add validation on the router, this is my

/* Handle Registration 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
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 */'/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 Source

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(...) */
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download