NodeJS express-validator with passport

若如初见. 提交于 2019-12-10 20:54:08

问题


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.


回答1:


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



回答2:


Putting validation code in LocalStartegy should work but, personally I would first do authentication and once it has passed use passport.

Consider something 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
    }
});



回答3:


/* 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:


I had the the same problem exactly. I just used a middleware for checking validation, where i call next() in case 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
  })
);


来源:https://stackoverflow.com/questions/27056195/nodejs-express-validator-with-passport

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!