我遇到问题,如果我尝试使用没有用户名或密码登录,我的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
,只要有GOT HERE
的帖子请求,就应该打印/login
。
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
没有值,我也希望调用此函数,以便我可以进行自定义错误处理。
我怎样才能做到这一点?
答案 0 :(得分:3)
您可以添加被调用的中间件befure身份验证策略。像这样:
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
})
在这个中间件中你可以做一些自定义错误处理