简体   繁体   English

如果字段为空,Passport.js不会调用LocalStrategy

[英]Passport.js not calling LocalStrategy if fields are empty

I'm having a problem where if I try to login with no username or password my passport.use function isn't being called at all. 我在哪里,如果我尝试没有用户名或密码我登录的问题passport.use功能不会被调用的。

Below is my express post route that runs passport.authenticate . 以下是我的运行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
    })

And below is my passport.use that should print GOT HERE whenever there is a post request to /login . 下面是我的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");

This works fine if email and password have some type of value. 如果emailpassword具有某种类型的值,则此方法很好用。 But I would like this function to be called even if there is no value for email and password so that I can do custom error handling. 但是,即使emailpassword没有值,我也希望调用此函数,以便我可以执行自定义错误处理。

How can I achieve this? 我该如何实现?

You could add middleware befure authentication strategy being called. 您可以添加称为中间件身份验证策略的中间件。 Something like this: 像这样:

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
})

And in this middleware you could do some custom error handling 在此中间件中,您可以执行一些自定义错误处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM