NodeJS Express Validator with Passport

I am writing a registration form table and I am using passport-local

but I want to add express-validator

to validate my form data. I am adding validation on router, this is my code router/index.js

:

/* 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
    });
  }
});

      

Validation is a job, but if successful, then the web page will load for a long time and not respond. I was looking for a passport document but didn't want to correct it.

This is the origin code, it works

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

      

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

+3


source to share


4 answers


Doing the check in LocalStrategy and not the router should work fine:



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

      

0


source


Entering the verification code into LocalStartegy should work, but personally I would authenticate first and after it has passed the Usage Passport.

Consider the following:



router.post('/login',function(req,res){

    req.checkBody('username', 'Username is required').notEmpty();
    req.checkBody('password', 'Password is required').notEmpty();

    //validate 
    var errors = req.validationErrors();

    if (errors) {

        res.render('signup',{user:null,frm_messages:errors});

    }
    else{
        passport.authenticate('login',{
            successRedirect:'/',
            failureRedirect: '/login',
            failureFlash : true  
        })(req,res); // <---- ADDD THIS
    }
});

      

+9


source


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

function checkEmail(req, res, next){
    req.checkBody('username', 'Username is required').notEmpty();
    req.checkBody('password', 'Password is required').notEmpty();
    //validate 
    var errors = req.validationErrors();
    if (errors) {
        res.render('signup',{user:null,frm_messages:errors});
    } else {
      next();
    }
}

      

+4


source


I had exactly the same problem. I just used a validation middleware where I call next () in case the validation is successful.

router.post("/login", 
  [check('email').isEmail().withMessage("A valid email is required")], 
  (req, res, next) => {
    // Check validation.
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      // handle your error (redirect, render, ..).
    }
    // if validation is successful, call next() to go on with passport authentication.
    next();
  },
  passport.authenticate("login", {
    successRedirect: "/",
    failureRedirect: "/login",
    failureFlash: true
  })
);

      

0


source







All Articles