Expo Google auth cancel causes error instead of returning as cancelled in iOS

Viewed 528

I am using Expo 37 with expo-google-app-auth 8.1.0. I am able to sign in users successfully. But in iOS, when a user clicks "cancel", whether in the Alert or the browser window, I get an error:

ERR_APP_AUTH: The operation couldn’t be completed. (org.openid.appauth.general error -3.)

This happens for both the simulator and standalone apps - again only for iOS. Why isn't it just returning an object with "type" : "cancel"?

Implementation of Google login method below:

    signInWithGoogle = async (): Promise<void> => {
        try {
            const result = await Google.logInAsync({
                androidClientId: ANDROID_CLIENT_ID,
                iosClientId: IOS_CLIENT_ID,
                androidStandaloneAppClientId: ANDROID_STANDALONE_CLIENT_ID,
                iosStandaloneAppClientId: IOS_STANDALONE_CLIENT_ID,
                scopes: ['profile', 'email'],
            });

            if (result.type === 'success') {
                await firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL);
                const credential = firebase.auth.GoogleAuthProvider.credential(result.idToken, result.accessToken);
                const googleProfileData = await firebase.auth().signInWithCredential(credential);
                if (googleProfileData.user.uid) {
                    this.props.setShouldBeLoggedOut(false);
                    this.props.setShouldPerformLogout(false);
                } else {
                    Alert.alert('Unable to sync Google credentials with Authentication server');
                }
            }
        } catch (error) {
            Alert.alert('Google Login Error:', error.message);
        }
    };
2 Answers

If you remove Alert.alert('Google Login Error:', error.message);, App will not crash. I think it might have to do something with error.message?

I ran into the same issue. The best I could do was to check e.code before raising the Alert:

    try {
      const { type, accessToken } = await Google.logInAsync({
        androidClientId: GOOGLE_ANDROID_CLIENT_ID,
        androidStandaloneAppClientId: GOOGLE_ANDROID_STANDALONE_CLIENT_ID,
        iosClientId: GOOGLE_IOS_CLIENT_ID,
        iosStandaloneAppClientId: GOOGLE_IOS_STANDALONE_CLIENT_ID,
      });
      if (type === "success") {
        this._handleAppLogin(accessToken);
      }
    } catch (e) {
      if (e.code != -3) {
        Alert.alert("Google Login Error", e.message);
      }
    }

However, I couldn't find documentation that specified what error codes were possible under what conditions, so this solution might suppress other real errors.

Related