ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-05-20 00:23:32  阅读:172  来源: 互联网

标签:javascript node-js express passport-js


我不确定这是否可行,但我想使用多种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,node-js,express,passport-js
来源: https://codeday.me/bug/20190519/1138727.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有