2014-11-06 51 views
3

我不知道这是可能的,但我想,以使用不同的设置取决于链接/用户范围的使用多个谷歌的策略。护照的NodeJS - 使用多个谷歌战略

我创建了两个独立的护照变量:

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值。

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

+1

作为一个方面说明。 'require'只被调用一次。随后的调用返回初始require调用的缓存版本。 – Maroshii 2014-11-06 21:54:19

回答

1

这是解决通过创建两个中间件功能来定义不同的路径上的护照变量:

app.get('/auth',middlefunc,passport.authenticate('google',['scopes'])) 
app.get('/joinauth',middlefunc2,passport.authenticate('google',['scopes'])) 


function middlefunc(req,res,next){ 
    passport.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}]); 
    }); 
    } 
)) 
    } 

function middlefunc2(req,res,next){ 
    //another definition of passport.use 

} 

没有必要再拍passport变量。

1

稍微简洁的方法是使用不同的名称设置单独的身份验证点,而不是在每个请求上覆盖已注册的策略。默认情况下,护照给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'])) 
+0

这是我很长一段时间寻找的东西。谢谢:) – Zub 2017-09-05 06:26:46