passport-google-oauth Failed to fetch user profile ECONNRESET
Asked Answered
P

4

6

login via google on my meanjs website stopped working all of the sudden after three years, without any change to the code. The request sent:

/**
 * Module dependencies
 */
var passport = require('passport'),
  GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
  users = require('../../controllers/users.server.controller');

module.exports = function (config) {
  // Use google strategy
  passport.use(new GoogleStrategy({
    clientID: config.google.clientID,
    clientSecret: config.google.clientSecret,
    callbackURL: config.google.callbackURL,
    passReqToCallback: true,
    scope: ['profile', 'email']
  },
    function (req, accessToken, refreshToken, profile, done) {
      // Set the provider data and include tokens
      var providerData = profile._json;
      providerData.accessToken = accessToken;
      providerData.refreshToken = refreshToken;
      // Create the user OAuth profile
      var providerUserProfile = {
        firstName: profile.name.givenName,
        lastName: profile.name.familyName,
        displayName: profile.displayName,
        email: profile.emails[0].value,
        username: profile.username,
        profileImageURL: (providerData.picture) ? providerData.picture : undefined,
        provider: 'google',
        providerIdentifierField: 'id',
        providerData: providerData
      };

      // Save the user OAuth profile
      users.saveOAuthUserProfile(req, providerUserProfile, done);
    }));
};

this successfuly redirects to google consent screen, after login the call back is fired:


  passport.authenticate('google', {},function (err, user, info) {
    if (err) {
      console.log(err);
      return res.redirect('/authentication/signin?err=' + encodeURIComponent(errorHandler.getErrorMessage(err)));
    }
    if (!user) {
      return res.redirect('/authentication/signin');
    }
.....

and returns the error "Failed to fetch user profile", this is the console.log of the err var:


{ InternalOAuthError: Failed to fetch user profile
    at C:\Users\Administrator\Documents\printer\mean-printer\node_modules\passport-google-oauth20\lib\strategy.js:99:19
    at ClientRequest.<anonymous> (C:\Users\Administrator\Documents\printer\mean-printer\node_modules\oauth\lib\oauth2.js
:162:5)
    at emitOne (events.js:116:13)
    at ClientRequest.emit (events.js:211:7)
    at TLSSocket.socketErrorListener (_http_client.js:401:9)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)
    at emitErrorNT (internal/streams/destroy.js:66:8)
    at _combinedTickCallback (internal/process/next_tick.js:139:11)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)
  name: 'InternalOAuthError',
  message: 'Failed to fetch user profile',
  oauthError: { Error: read ECONNRESET
    at TLSWrap.onread (net.js:622:25) errno: 'ECONNRESET', code: 'ECONNRESET', syscall: 'read' } }

Again saying, this started suddenly after working for 3 years. no code changes were made.

any ideas?

Cheers

Parsimony answered 16/6, 2022 at 9:44 Comment(1)
We have the same problem which started when we added a second web application, served on a different port, but uses the same clientID, clientSecret and a different callbackURL (as there's a different backend service that handles it)Glairy
S
0

Trying many things (including changing the oauth to version 0.10.0 vs. 0.9.x , suggested on several page... but that does not work)

The solution which worked was to change the Node version to 19 (18.12.0 and 18.12.1 face the issue).

Sensational answered 26/10, 2022 at 1:45 Comment(0)
C
0

On node 18.12.1 same issue, upgrade to 19.2.0 fix this problem. Probably downgrading to lower versions will fix it too.

Circe answered 8/12, 2022 at 20:44 Comment(0)
S
0

Node v16.13.2 is also affected by this. Some of the requests would fail as you said with Failed to fetch user profile Upgrading to Node v19 fixed the issue here as well.

Here's an issue for this bug: https://github.com/jaredhanson/passport-google-oauth2/issues/87

Subserve answered 27/12, 2022 at 17:26 Comment(0)
P
0

Upgraded to Node.js lts/hydrogen (v18.15.0-1 at the time of writing) and it seemed to do the trick.

Pteridophyte answered 23/4, 2023 at 5:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.