此身份验证工作正常
app.post('/login', passport.authenticate('local-login', {
successRedirect: '/home',
failureRedirect: '/login',
failureFlash: true
})
);
但我试图在使用express-validator进行身份验证之前验证表单的字段。
我是带着这个来的
app.post('/login', function(req, res){
req.checkBody('email', 'Email is required').notEmpty();
req.checkBody('email', 'Email is not valid').isEmail();
req.checkBody('password', 'Password is required').notEmpty();
var validationErr = req.validationErrors();
if (validationErr){
res.render('login', {
errors: validationErr,
failureFlash: true
});
} else {
// authenticate once fields have been validated
passport.authenticate('local-login', {
successRedirect: '/home',
failureRedirect: '/login',
failureFlash: true // allow flash messages
})
}
});
使用第二个代码,当我提交表单时没有任何反应,并且客户端在一段时间后给出错误消息localhost没有发送任何数据。第一部分工作正常,当我提交空表单并达到验证方法时,我可以看到所有错误。我怀疑这个question可能部分回答了我的问题或者有点相关,但我无法理解。
passport.js documentation提供了一个带有函数的示例,但仅当身份验证成功时才会调用该函数,因此之后。我想在身份验证之前执行字段验证。
如果您需要护照验证码,请告诉我。
最佳答案
passport.authenticate
是一个函数。在您的第一个(工作)代码中,您将其称为中间件,其中它传递(req、res、next)的对象作为参数。
使用第二个代码,您尝试直接调用它而不带参数,并且客户端由于没有收到响应而超时。
如果我没有遗漏什么,您也许可以通过将 (req, res) 传递给它来完成这项工作,如下所示:
if (validationErr){
res.render('login', {
errors: validationErr,
failureFlash: true
});
} else {
// authenticate once fields have been validated
passport.authenticate('local-login', {
successRedirect: '/home',
failureRedirect: '/login',
failureFlash: true // allow flash messages
})(req, res, next);
}
关于javascript - 护照验证卡住并且不返回任何值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53254274/