我正在使用护照.js用于Facebook和Google身份验证。Facebook 身份验证策略工作正常,回调中的 res.redirect 只被调用一次。但是对于谷歌身份验证,我不知所措,因为它被称为两次。我花了几个小时试图调试它并查看护照源代码,但找不到错误。
我的谷歌身份验证只是从Gmail获得一些联系人。
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 调用进行了两次,即使之前只调用了一次控制台.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 降压......从查看这是一个旧的 https://bugs.chromium.org/p/chromium/issues/detail?id=64810,我现在正在检查如何修复它。
我有同样的问题。但是当我看到这个并停止广告拦截时,问题就解决了。