【问题标题】:Routes file issues with PassportPassport 的路由文件问题
【发布时间】:2013-03-04 19:40:03
【问题描述】:

我正在使用 NodeJS、Express 和 PassportJS 构建 Web 应用程序。我的一条路线有问题,我无法理解。当我有:

...
app.get('/auth/facebook', passport.authenticate('facebook'));
...

似乎一切正常。但是当我将其更改为:

...
app.get('/auth/facebook',
  function(req, res) {
    passport.authenticate('facebook');
  });
...

挂了?我在 app.get 功能上遗漏了什么吗?我希望能够做到这一点,因为我想让路径更加动态,我可以在其中确定哪些护照进行身份验证。例如:

...
app.get('/auth/:provider',
  function(req, res) {
    passport.authenticate(req.params.provider);
  });
...

提供者可以是facebooktwittergoogle...

【问题讨论】:

    标签: node.js authentication express routes passport.js


    【解决方案1】:

    passport.authenticate 是中间件,看源码:https://github.com/jaredhanson/passport/blob/master/lib/passport/middleware/authenticate.js

    passport.authenticate('facebook') 返回一个函数,该函数从 express 获取 req、res、next 参数并处理它们。

    所以:

    app.get('/auth/:provider',
      function(req, res, next) {
        passport.authenticate(req.params.provider)(req, res, next);
      });
    

    是你需要的。

    【讨论】:

      猜你喜欢
      • 2019-10-28
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-09
      • 2019-04-26
      • 1970-01-01
      相关资源
      最近更新 更多