Node.js res.redirect在passport google回调中被调用了两次-我无法找到原因
我正在Facebook和Google auth上使用passport.js。FacebookAuth策略工作正常,回调中的res.redirect只被调用一次。但对于谷歌认证,我感到不知所措,因为它被称为两次。我花了好几个小时试着调试它,并查看passport源代码,但就是找不到bug 我的GoogleAuth刚刚从Gmail获得了一些联系人Node.js res.redirect在passport google回调中被调用了两次-我无法找到原因,node.js,passport.js,passport-google-oauth,passport-google-oauth2,Node.js,Passport.js,Passport Google Oauth,Passport Google Oauth2,我正在Facebook和Google auth上使用passport.js。FacebookAuth策略工作正常,回调中的res.redirect只被调用一次。但对于谷歌认证,我感到不知所措,因为它被称为两次。我花了好几个小时试着调试它,并查看passport源代码,但就是找不到bug 我的GoogleAuth刚刚从Gmail获得了一些联系人 app.get('/contacts/google', passport.authenticate('google', { session: f
app.get('/contacts/google',
passport.authenticate('google', { session: false, scope: ['profile', 'email', 'https://www.googleapis.com/auth/contacts.readonly'] })
);
app.get('/login/google/callback',
passport.authenticate('google', { session: false, failureRedirect: '/' }),
function(req, res, next) {
process.nextTick(function() {
console.log("Right before the googletoken call", req.user);
res.redirect("/users/" + '?googletoken=' + req.user.access_token );
});
}
);
谷歌战略
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var googleConfig = require('./googlekeys.js');
module.exports = function(passport) {
passport.use('google', new GoogleStrategy({
clientID : googleConfig.appID,
clientSecret : googleConfig.appSecret,
callbackURL : googleConfig.callbackUrl,
profileFields: ['email','profile']
},
// google will send back the tokens and profile
function(access_token, refresh_token, profile, done) {
process.nextTick(function() {
//we send the token we receive back so we can use it to get the contacts
console.log("Before calling the token callback");
var user = {};
user.access_token = access_token;
return done(null, user);
});
}
));
};
这里是从控制台-问题是/users/?googletoken调用了两次,即使前面的console.log只调用了一次
GET /contacts/google 302 2.739 ms - 0
At the beginning
Before calling the token callback
Right before the googletoken call { access_token: '[GOOGLE TOKEN]' }
GET /login/google/callback?code=[CODE] 302 485.728 ms - 348
GET /users/?googletoken=[GOOGLE TOKEN THAT WAS RECEIVED BACK] 200 2.812 ms - 7791
GET /users/?googletoken=[GOOGLE TOKEN THAT WAS RECEIVED BACK] 304 1.575 ms - -
在最后考虑在另一个浏览器中尝试后,我发现Safari和Firefox没有问题,所以看起来这是一个Chrome浏览器。。。从这是一个老问题来看,我现在正在检查如何修复它。我也有同样的问题。但当我查看并停止adblock时,问题已经解决。+1。我希望我能不止一次投票来引起人们对这个问题的关注——六个月后,仍然没有可靠的解决方案或解释。在我的例子中,第一个操作(您的“/contacts/google”)被调用了两次,导致两个同时进行的身份验证流和两次尝试的回调。