微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – Nodejs Passport – 使用多个Google策略

我不确定这是否可行,但我想使用多种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 @R_76_4045@ion                                
        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 @R_76_4045@ion                                
        //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']))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐