具有多个身份验证提供程序的passport.js? [英] passport.js with multiple authentication providers?

查看:110
本文介绍了具有多个身份验证提供程序的passport.js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Passport.js是否可以为同一路由指定多个身份验证提供程序?

Using Passport.js is there a way for me to specify multiple authentication providers for the same route?

例如(摘自护照指南),我可以在以下示例路线中使用本地策略,facebook策略和twitter策略吗?

For example (from passport's guide) can I use local and facebook and twitter strategies on the sample route below?

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

推荐答案

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

Passport's middleware is built in a way that allows you to use multiple strategies in one passport.authenticate(...) call.

但是,它是用OR顺序定义的.也就是说,只有在所有策略均未成功返回的情况下,它才会失败.

However, it is defined with an OR order. This is, it will only fail if none of the strategies returned success.

这是您将如何使用它:

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

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

In other words, the way to use it, is passing an array containing the name of the strategies you want the user to authenticate with.

此外,别忘了预先设置要实施的策略.

Also, dont forget to previously set up the strategies you want to implement.

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

You can confirm this info in the following github files:

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

护照的authenticate.js定义

这篇关于具有多个身份验证提供程序的passport.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆