0

我指的是以下示例:

https://github.com/jaredhanson/passport-local/blob/master/examples/login/app.js

特别是以下代码:

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

发布的代码工作得很好。但是,如果我尝试通过执行以下操作来重构它:

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

function authenticate() {
    return passport.authenticate('local', { failureRedirect:'/fail', failureFlash:true });
}

事情不再起作用了。我究竟做错了什么?

4

1 回答 1

1

我认为您必须调用authenticate()(使用括号)才能执行该函数并返回中间件。例如:

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

您当前设置它的方式, authenticate 是作为路由处理程序安装的,因此 express 使用req,res参数(未使用或响应)调用它。

于 2012-05-07T01:56:33.717 回答