Code 47 error on realm.logIn(credentials) with Google oAuth

I’m trying to use realm.login.

  1. I’m using expo-auth-session/providers/google for getting the oAuth 2.0 access_code
  2. The Google oAuth response params have access_token, code, id_token, etc.
  3. I’m trying to use the access_token, id_token, or code for creating the credential for using realm.login()
  4. The error code is 47 and message is
    “error exchanging access code with OAuth2 provider” or “error fetching info from OAuth2 provider”.

`
const [request, response, promptAsync] = Google.useAuthRequest({
expoClientId: process.env.GOOGLE_EXPO_CLIENT_ID,
androidClientId: process.env.GOOGLE_ANDROID_CLIENT_ID,
iosClientId: process.env.GOOGLE_IOS_CLIENT_ID,
webClientId: process.env.GOOGLE_WEB_CLIENT_ID,
});

useEffect(async () => {
// try {
if (response?.type === “success”) {
const idToken = response.params.id_token;
const { access_token, code } = response.params;

  if (idToken) {
    const credentials = Realm.Credentials.google({
      idToken,
      // authCode: code,
      // idToken: access_token,
    });

    realm
      .logIn(credentials)
      .then((user) => {
        console.log(`Logged in with id: ${user.id}`);
      })
      .catch((err) => {
        console.log("@realm.login: err", err);
      });
  }
}

}, [response]);
`
With the above code, if I do console.log(credentials) it’s an empty object. Is it normal?

For Realm,

  1. I turned on Google for [Authentication Providers] and added client ID and secret from Google console for the web application credential for Realm.
  2. Open ID is off

and Google console.
I have the web application credential for Realm;
Authorized JavaScript origins: https://realm.mongodb.com
Authorized Redirect URIs: https://ap-southeast-1.aws.stitch.mongodb.com/api/client/v2.0/auth/callback
And 4 more credentials (Web application for Expo Go Proxy, Web application for web client, iOS, and Android)

And also have /auth/providers.json (I’m not sure it’s needed even I set it on the Realm UI page)
{ "oauth2-google": { "name": "oauth2-google", "type": "oauth2-google", "disabled": false, "config": { "clientId": process.env.GOOGLE_CLIENT_ID, "openId": false }, "secret_config": { "clientSecret": process.env.GOOGLE_CLIENT_SECRET }, "metadata_fields": ["name", "first_name", "last_name", "picture", "email" ], "redirect_uris": [], "domain_restrictions": [] } }

FYI,
If I run this code with the access_token, I can receive the user data without any issue.
const getUserInfo = async (token) => { let userInfoResponse = await fetch( "https://www.googleapis.com/oauth2/v2/userinfo", { headers: { Authorization:Bearer ${token}` },
},
).catch((err) => console.log(“@getUserInfo: err”, err));

userInfoResponse
  .json()
  .then((data) => {
    console.log("@userInfoResponse > data:", data);
    const user = data;
  })
  .catch((err) => console.log("@userInfoResponse: err", err));

};`

1 Like

Did you figure out the issue here?

Hello @Try_Catch_Do_Nothing ,

Same here … did you ever manage to solve it ?

Best regards,
Rasvan

I don’t recall if I did or not. I ended up abandoning MongoDB in favor of PostgreSQL/Supabase last year.

Hey,

Thank for the quick replay … i am extremely frustraded about this … i am triing to implement it on IOS with a working version on Android … from what i read there seems to be a problem when you need to implement it on both IOS and Android.

Unfortunately it is not an option for us because the app is in production with a 23k userbase.

If you ever remeber the outcome … any help would be much apreciated.

Best regards,
Rasvan

Posted a workaround on StackOverflow, see Cannot set up Google Auth on iOS and Android at the same time.