Skip to content

Getting ECONNRESET in passport-google-oauth20 strategy #368

@Rishabh570

Description

@Rishabh570

This is my Google OAuth 2.0 passport option:

options:  {
  clientID: 'my-client-id'
  clientSecret: 'my-client-secret',
  callbackURL: '/api/auth/google/callback',
  scope: [ 'profile', 'email' ],
  passReqToCallback: true,
  state: true
}

Here's my Google passport strategy:

var passport = require('passport');
var GoogleStrategy = require('passport-google-oauth20').Strategy;

var User = require('@models/user');
var config = require('@config');

passport.use(new GoogleStrategy({
  clientID: config.googleClientId,
  clientSecret: config.googleClientSecret,
  callbackURL: config.googleCallbackUrl,
  scope: ['profile', 'email'],
  passReqToCallback: true,
  state: true,
  },
  function(req, accessToken, refreshToken, profile, done) {
    console.log('profile: ', profile);
    try {
      var searchQuery = {
        name: profile.displayName
      };
  
      // store user info in mongo
      User.findOne(searchQuery, function(err, user) {
        if(err) {
          console.log('[google] err: ', err);
          return done(err);
        } else if (user !== null) {
          console.log('google user already there');
          return done(null, user);
        }
        console.log("user not present, creating one...");
        user = new User({
          name: profile.displayName,
          email: profile['_json']['email'],
          profileId: profile.id,
          google: {
            accessToken,
            profileId: profile.id,
            email: profile['_json']['email']
          }
        });
        user.save(function(err) {
          if(err) {
            console.log('err occurred while saving google user: ', err);
            console.log(err);  // handle errors!
          } else {
            console.log("saving user ...");
          }
        });
      });
    } catch (verifyErr) {
      done(verifyErr);
    }
  }

));

module.exports = passport;

I've checked by logging the values in passport-oauth2 and passport-google-oauth20. Here's what I found:

  1. The execution is going here with everything as expected (profile contains the expected data)
  2. The control then goes to my passport google strategy file (mentioned above). Profile and access token being correct here as well.
  3. But the issue is happening right after the above step, the execution is going right to the Strategy.userProfile function and the err block having the message of CONNRESET ECONNRESET Screenshot 2022-04-30 at 18 13 53
  4. This leads me to this error and the server exits. All the server code is exactly the same for github oauth but it works without any errors. This makes me think there's some issue with google's OAuth package?

Opening the issue here because the issue can also be in the request implementation and related to keep-alive timeout?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions