58

使用 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);
  });
4

1 回答 1

111

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 文件中确认此信息:

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

Passport 的 authenticate.js 定义

于 2013-12-23T16:03:09.360 回答