Passport Authenticate不会重定向

问题描述:

我正在写一个本地注册策略并注意到它不起作用所以我退后一步,尝试对我的空集合进行身份验证。每次我提交表单都需要大约30-40秒才能导致超时。我确保调用了passport.authenticate(),但似乎它没有进行任何重定向,因此它超时,因为我也没有渲染任何东西。

I was writing a local-signup strategy and noticed that it doesn't work so I stepped back and tried to authenticate against my empty collection. Every time I submit the form it takes ~30-40s until it results in a timeout. I ensured passport.authenticate() is called but it seems ike it's not doing any redirects and hence it is timing out because I am not rendering something either.

问题:


  1. 我预计它会重定向到failureUrl(这是'/ signup'),而是一切都没有发生。我在这里做错了什么?

  2. 为什么没有来自护照的单一日志消息?这让我很生气,因为我完全不知道那里出了什么问题。

  3. 我是node.js的新手,据我所知,我不需要通过配置护照对象到路由器,但我只能做 const passport = require('passport')是正确的吗?

  1. I expected that it would do a redirect to the failureUrl (which is '/signup'), but instead nothing is happening. What am I doing wrong here?
  2. Why there is no single log message coming from passport? This is driving me crazy because I have absolutely no idea what is going wrong there.
  3. I am new to node.js and as far as I got I don't need to pass the configured passport object to the router but instead I can just do const passport = require('passport') is that correct?

这是/ signup路线的函数处理程序:

function processSignup (req, res) {
    logger.info("POST request received")
    logger.info(req.body)
    passport.authenticate('local', {
        successRedirect : '/profile', // redirect to the secure profile section
        failureRedirect : '/signup', // redirect back to the signup page if there is an error
        failureFlash : true // allow flash messages
    })
}

Winston打印:


7:32:04 PM - info:POST request recei ved 7:32:04 PM - info:
username = dasass @ das.de,password = dasdsa,submit = Register

7:32:04 PM - info: POST request received 7:32:04 PM - info: username=dassd@dass.de, password=dasdsa, submit=Register

我的passport.js文件如下所示:

const LocalStrategy = require('passport-local').Strategy
const User = require('./user-model')
const passport = require('passport')

// expose this function to our app using module.exports
function config() {
    passport.serializeUser(function(user, done) {
        done(null, user.id)
    })

    // used to deserialize the user
    passport.deserializeUser(function(id, done) {
        User.findById(id, function(err, user) {
            done(err, user)
        })
    })

    passport.use(new LocalStrategy(
        function(username, password, done) {
            User.findOne({ username: username }, function(err, user) {
                if (err) { return done(err); }
                if (!user) {
                    return done(null, false, { message: 'Incorrect username.' });
                }
                if (!user.validPassword(password)) {
                    return done(null, false, { message: 'Incorrect password.' });
                }
                return done(null, user);
            });
        }
    ));
}

module.exports = {
    config: config
}

相关剪辑我的app.js:

// required for passport
require('./authentication/passport').config();
app.use(cookieParser())
app.use(bodyParser())
app.use(session({ 
    secret: 'secretToBeChanged',
    saveUninitialized: false,
    resave: false
}))
app.use(passport.initialize())
app.use(passport.session()) // persistent login sessions
app.use(flash()) // use connect-flash for flash messages stored in session


在快速查看了passportjs的文档之后,我认为你需要做这样的事情:

After a quick look at the documentation for passportjs, I think you need to do something like this:

function processSignup (req, res, next) {
    logger.info("POST request received")
    logger.info(req.body)
    const handler = passport.authenticate('local', {
        successRedirect : '/profile', // redirect to the secure profile section
        failureRedirect : '/signup', // redirect back to the signup page if there is an error
        failureFlash : true // allow flash messages
    });
    handler(req, res, next);
}

passport.authenticate()返回一个用作路由处理函数的函数。

通常,你可以输入类似的东西:

passport.authenticate() returns a function that is meant to be used as the route handler function.
Normally, you would type something like:

app.post('/login', passport.authenticate('local', {
  successRedirect: '/',
  failureRedirect: '/login',
  failureFlash: true 
}));

但是既然你已经使用自己的路由处理函数进行了抽象,那么你需要调用从 passport.authenticate()

But since you have abstracted with your own route handler function, you need to invoke the one returned from passport.authenticate().