passport.js与多个身份validation提供程序?
使用Passport.js有没有办法让我为相同的路由指定多个身份validation提供程序?
例如(从护照指南)我可以在下面的示例路线上使用本地和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); });
护照中间件的构build方式允许您在一个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); });
换句话说,使用它的方式是传递一个数组,其中包含您希望用户进行身份validation的策略的名称。
另外,不要忘记以前build立你想实施的策略。
你可以在下面的github文件中确认这个信息:
在multi-auth示例中使用基本或摘要进行身份validation。
Passport的authenticate.js定义