javascript – Nodejs Passport – 使用多个Google策略
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – Nodejs Passport – 使用多个Google策略,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2833字,纯文字阅读大概需要5分钟。
内容图文
我不确定这是否可行,但我想使用多种Google策略,以便根据链接/用户使用不同的范围集.
我创建了两个独立的护照变量:
passport = require('passport')
passport2 = require('passport')
我已将它们设置如下:
passport.use(new GoogleStrategy({
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/callback"
},
function(accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function (){
// Changing this to return the accessToken instead of the profile information
console.log(profile.displayName);
return done(null, [{token:accessToken,rToken:refreshToken,'profile':profile}]);
});
}
));
passport2.use(new GoogleStrategy({
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: "http://localhost:3000/join/callback"
},
function(accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function (){
// Changing this to return the accessToken instead of the profile information
//console.log(profile);
return done(null, [{token:accessToken,rToken:refreshToken,'profile':profile}]);
});
}
))
对于我的路线我有这个:
app.get('/auth',
passport.authenticate('google', {scope: ['scopes'],
accessType:'offline', approvalPrompt:'force'})
);
app.get('/joinreq',
passport2.authenticate('google', {scope: ['different_scopes]})
);
我的回调看起来像这样:
app.get('/join/callback', function(req,res){
console.log('made it to the join callback');
res.redirect('/great')
}
app.get('/auth/callback', function(req,res){
console.log('made it to the auth callback');
res.redirect('/index')
}
我能够成功地对每个作用域成功进行身份验证 – 我遇到的问题是我的回调只会转到/ join / callback.
似乎变量passport2正在覆盖护照的价值.
有什么方法可以解决这个问题吗?我想为管理员用户提供一组范围,为其他人提供一组范围.
解决方法:
稍微清晰的方法是使用单独的名称设置单独的身份验证点,而不是在每个请求上覆盖已注册的策略.默认情况下,passport会向GoogleStrategy提供“google”名称,但您可以指定其他名称作为安装第二个策略的第一个参数.
// set up first Google strategy
passport.use('google', new GoogleStrategy({
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: "http://localhost:3000/join/callback"
}, function(accessToken, refreshToken, profile, done) {
...
}
)
// Second strategy -- could use different callback URL, etc.
passport.use('google-alt', new GoogleStrategy({
...
});
app.get('/auth', passport.authenticate('google', ['scopes']))
app.get('/joinauth', passport.authenticate('google-alt', ['scopes']))
内容总结
以上是互联网集市为您收集整理的javascript – Nodejs Passport – 使用多个Google策略全部内容,希望文章能够帮你解决javascript – Nodejs Passport – 使用多个Google策略所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。