passport.js有多个身份验证提供程序?

cgi*_*omi 40 node.js passport-facebook passport.js

使用Passport.js有一种方法可以为同一路由指定多个身份验证提供程序吗?

例如(来自护照指南)我可以在下面的样本路线上使用本地和Facebook和Twitter策略吗?

app.post('/login',
  passport.authenticate('local'), /* how can I add other strategies here? */
  function(req, res) {
    // If this function gets called, authentication was successful.
    // `req.user` contains the authenticated user.
    res.redirect('/users/' + req.user.username);
  });
Run Code Online (Sandbox Code Playgroud)

Dan*_*rez 84

Passport的中间件以一种允许您在一次passport.authenticate(...)调用中使用多种策略的方式构建.

但是,它是用OR顺序定义的.也就是说,如果没有一个策略返回成功,它将只会失败.

这是你如何使用它:

app.post('/login',
  passport.authenticate(['local', 'basic', 'passport-google-oauth']), /* this is how */
     function(req, res) {
       // If this function gets called, authentication was successful.
       // `req.user` contains the authenticated user.
       res.redirect('/users/' + req.user.username);
});
Run Code Online (Sandbox Code Playgroud)

换句话说,使用它的方法是传递一个数组,其中包含您希望用户进行身份验证的策略的名称.

此外,不要忘记以前设置您想要实施的策略.

您可以在以下github文件中确认此信息:

在多身份验证示例中使用基本或摘要进行身份验证.

Passport的authenticate.js定义

  • 我无法弄清楚这是如何有用的.基本策略需要在每个请求中完成,至少要像卷曲一样工作.本地策略将保留浏览器的会话.因此,如果我想拥有一个通过登录为浏览器进行会话的API,但BasicAuth通过curl为每个请求提供凭据,我似乎无法做到这一点.那可能吗? (4认同)