react-nativeexpoexpo-auth-session

Expo AuthSession does not work on the first try


I am using expo-auth-session to create login screen with Facebook and Google. The issue is gglResponse (full code snippet below) is null on the first try. If I call the gglPromptAsync() again the second time, it would resolve to success, assuming the user is able to authenticate themselves with the provider.

I had tried console.log(AppState.currentState) on different screens before gglPromptAsync() as it is suggested here.

I had tried adding useProxy: true and showInRecents: true as suggested here and here

It's quite disappointing that someone has proposed a solution more than 3 years ago and the behavior is still here.

Here is the code snippet with Google. Facebook is exactly the same.

import * as Google from 'expo-auth-session/providers/google';

const [gglRequest, gglResponse, gglPromptAsync] = Google.useAuthRequest({
  expoClientId: mobileSettings.expoClientId,
  iosClientId: mobileSettings.iosClientId,
  androidClientId: mobileSettings.AndroidClientId
});

const onSignInPressed = async (ssoType: SSOType) => {
  await gglPromptAsync({ showInRecents: true, useProxy: true });

  if (gglResponse?.type === 'success') { // gglResponse is null on the first request
    const { authentication } = gglResponse;
    await login(ssoType, authentication?.accessToken || "");
  } else { return; }
};

Solution

  • To those who are having this same issue, you can try using the response in the promptAsync call, instead of the gglResponse. Something like this, and it's working well for me.

    gglPromptAsync({ showInRecents: true }).then(async (response) => {
      if (response?.type === "success") {
        const { authentication } = response;
        await login(ssoType, authentication?.accessToken || "").then(() => {
          // other signin logic goes here...
         });
       } else {
         return;
       }
     });