我不确定是否可行,但是我想使用多种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的值。

有什么办法可以解决这个问题?我希望为管理员用户提供一组范围,为其他所有人提供一组范围。

最佳答案

一种更简洁的方法是使用单独的名称设置单独的身份验证点,而不是针对每个请求覆盖已注册的策略。默认情况下,password将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']))

07-28 02:43
查看更多