node.js 具有多个身份验证提供程序的passport.js?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20745296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
passport.js with multiple authentication providers?
提问by cgiacomi
Using Passport.js is there a way for me to specify multiple authentication providers for the same route?
使用 Passport.js 有没有办法为同一路由指定多个身份验证提供程序?
For example (from passport's guide) can I use local and facebook and twitter strategies on the sample route below?
例如(来自护照指南)我可以在下面的示例路线上使用本地和 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);
});
回答by Danilo Ramirez
Passport's middleware is built in a way that allows you to use multiple strategies in one passport.authenticate(...)call.
Passport 的中间件的构建方式允许您在一次passport.authenticate(...)调用中使用多种策略。
However, it is defined with an OR order. This is, it will only fail if none of the strategies returned success.
但是,它是用 OR 顺序定义的。也就是说,只有当所有策略都没有成功时,它才会失败。
This is how you would use it:
这是你将如何使用它:
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.
另外,不要忘记预先设置您要实施的策略。
You can confirm this info in the following github files:
您可以在以下 github 文件中确认此信息:
Authenticate using either basic or digest in multi-auth example.

