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

时间:2013-12-23 14:11:51

标签: 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);
  });

1 个答案:

答案 0 :(得分:76)

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

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

另外,不要忘记先前设置要实施的策略。

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

Authenticate using either basic or digest in multi-auth example.

Passport's authenticate.js definition