本文介绍了带护照的 NodeJS 快速验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个注册表单,我使用 passport-local 是可行的,但我想添加 express-validator 来验证我的表单数据.我在路由器上添加验证,这是我的 router/index.js 代码:

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.

这是源代码,它的工作

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

我想我可以使用jquery来结账,但我不这样做.因为我只是想尝试使用带护照的验证器.

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.

推荐答案

在 LocalStrategy 而不是路由器中进行验证应该可以正常工作:

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

这篇关于带护照的 NodeJS 快速验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-09 19:11
查看更多