Passportjs Google身份验证回调错误

时间:2016-11-28 02:07:25

标签: passport.js passport-google-oauth2

当我为Google登录调用我的回调路由时,我遇到了500 Internal Server服务器错误,我不确定我的代码的哪一部分导致了这个问题。正如您从终端结果中看到的那样,它正在传递if语句条件并触发响应并输出[object SequelizeInstance:external_account]的cb函数。输出可能是不正确的值吗?我不确定电话会接受什么价值。

控制台日志:

If statement was true externalId and Email present
GET /auth/google/callback?code=4/mvFTIURH5P0ACQFwZobC04-ftehalfdf4454 500 434.737 ms - 44
[object SequelizeInstance:external_account]

PassportJS设置:

/*====  Google Configuration  ====*/

passport.use(new GoogleStrategy({
    clientID: 'client-id',
    clientSecret: 'client-secret',
    callbackURL: 'http://localhost:3000/auth/google/callback'
  }, function(accessToken, refreshToken, profile, cb) {
        var user;
        models.User.findOne({
            where: {
                email: profile.emails[0].value
            }
        }).then(function(_user){
            user = _user;
            return models.ExternalAccount.findOne({
                where: {
                    externalSourceId: profile.id
                }
            }).then(function(externalAccount, err){
                if(user && externalAccount){
                console.log("If statement was true externalId and Email present");
                return cb(externalAccount, err)
            } else if (user){
                console.log("Else If statement was true Email was present, but no account Id");
                return models.ExternalAccount.create({
                    externalSource: "Google",
                    externalSourceId: externalAccount.externalSourceId,
                    userId: externalAccount.user.id
                }).then(function(){
                    return cb(externalAccount, err)
                });
            } else {
                console.log('Error');
            }
            })
        })
  }));

路线:

/*====  /AUTH/GOOGLE  ====*/

siteRoutes.route('/auth/google') 
    .get(passport.authenticate('google', { scope: ['profile', 'email'] }));

/*====  /AUTH/GOOGLE/CALLBACK  ====*/

siteRoutes.route('/auth/google/callback') 
    .get(passport.authenticate('google', {
            successRedirect : '/app',
            failureRedirect : '/login',
            failureFlash: 'Invalid Google credentials.'
        }),function(req, res){
        res.redirect('/app');
    });

1 个答案:

答案 0 :(得分:0)

如果您未使用https,则可能是Google的服务器发生了错误。 Google登录api仅接受使用ssl的安全网络。 尝试使用ssl。如果您仍然遇到此问题...

相关问题