[英]Passport.js not calling LocalStrategy if fields are empty
我在哪里,如果我尝试没有用户名或密码我登录的问题passport.use
功能不会被调用的。
以下是我的运行passport.authenticate
特快专递路线。
app.post('/login', passport.authenticate('local-login', {
failureRedirect: '/login', // redirect back to the login page if there is an error
failureFlash: true // allow flash messages
})
下面是我的passport.use
,当有/login
的发帖请求时,应该在GOT HERE
打印GOT HERE
。
passport.use('local-login', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) { // callback with email and password from our form
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
console.log("GOT HERE");
如果email
和password
具有某种类型的值,则此方法很好用。 但是,即使email
和password
没有值,我也希望调用此函数,以便我可以执行自定义错误处理。
我该如何实现?
您可以添加称为中间件身份验证策略的中间件。 像这样:
app.post('/login', function(req, res, next) {
// do custom error handling
}, passport.authenticate('local-login', {
failureRedirect: '/login', // redirect back to the login page if there is an error
failureFlash: true // allow flash messages
})
在此中间件中,您可以执行一些自定义错误处理
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.