如何处理 passport.js 中函数 "done" 的第三个参数?

How to handle thrid parameter of function "done" in passport.js?

我在 http://passportjs.org/docs 看到了示例代码,我们可以为 passport 的 done 函数传递第三个参数

代码:

var passport = require('passport')
  , LocalStrategy = require('passport-local').Strategy;

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

在这种情况下,他们正在传递 { message: 'Incorrect username.' }。

我的问题是如何处理这第三个参数。

编辑: 这是我的路由代码:

app.post('/login', 
  passport.authenticate('local', { failureRedirect: '/login' }),
  function(req, res) {
    res.redirect('/');
  });

我想知道如何使用从护照发送的消息,例如

res.render('myjade', {'message': **THAT MESSAGE**})

像这样

来自文档:

Redirects are often combined with flash messages in order to display status information to the user.

Setting the failureFlash option to true instructs Passport to flash an error message using the message given by the strategy's verify callback, if any.

Note: Using flash messages requires a req.flash() function. Express 2.x provided this functionality, however it was removed from Express 3.x. Use of connect-flash middleware is recommended to provide this functionality when using Express 3.x.

因此将您的身份验证调用修改为:

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

然后使用 connect-flash 中间件或定义您自己的中间件来提供 req.flash() 功能。