cgi*_*omi 40 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);
});
Run Code Online (Sandbox Code Playgroud)
Dan*_*rez 84
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);
});
Run Code Online (Sandbox Code Playgroud)
换句话说,使用它的方法是传递一个数组,其中包含您希望用户进行身份验证的策略的名称.
此外,不要忘记以前设置您想要实施的策略.
您可以在以下github文件中确认此信息: