Custom OAuth2 Social connection with Cognito returning error "Invalid user id"

Problem statement

We are setting up a Custom OAuth2 Social connection with Cognito, and when attempting to log in with it we saw an error page with “Invalid user id”.

Cause

This error was occurring because a fetch user profile script had not been implemented for the connection:

function(accessToken, ctx, cb) {
    const profile = {};
    // Call OAuth2 API with the accessToken and create the profile
    cb(null, profile);
  }

Solution

Properly configuring the fetch user profile script resolved this issue:

https://auth0.com/docs/authenticate/identity-providers/social-identity-providers/oauth2#fetch-user-profile-script