且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-05 22:06:40

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定义