Firebase not working for Expo GoogleSignIn

Please provide the following:

  1. SDK Version: 39
  2. Platforms(Android/iOS):

I am using Expo GoogleSignIn and wants to integrate it with firebase. I have followed the docs exactly as what it said. However, saving data to firebase Realtime Database not working for Google(Facebook works just fine) even though the login works as fine. All the data requested was successfully fetched. My code is as below.

  const initAsyncGoogle = async () => {
    try {
      await GoogleSignIn.initAsync({
        clientId:
          Platform.OS === 'android'
            ? process.env.EXPO_GSERVICES_ANDROID
            : process.env.EXPO_GSERVICES_IOS,
      });
      _syncUserWithStateAsync();
    } catch (err) {
      console.warn(err);
      // Alert.alert('Google login: Error:' + err);
    }
  };

  const _syncUserWithStateAsync = async () => {
    const user = await GoogleSignIn.signInSilentlyAsync();
    dispatch(setUserData(user));
  };
  const loginWithGoogle = async () => {
    try {
      await GoogleSignIn.askForPlayServicesAsync();
      const { type, user } = await GoogleSignIn.signInAsync(); 
      if (type === 'success') {
        await onSignIn(user); <----------------- Once user Object is available, I call the onSignIn method
        _syncUserWithStateAsync();
      }
    } catch ({ message }) {
      Alert.alert('Google login: Error:' + message);
    }
  };
  const isUserEqual = (googleUser: any, firebaseUser: any) => {
    if (firebaseUser) {
      const providerData = firebaseUser.providerData;
      for (let i = 0; i < providerData.length; i++) {
        if (
          providerData[i].providerId ===
            firebase.auth.GoogleAuthProvider.PROVIDER_ID &&
          providerData[i].uid === googleUser.getBasicProfile().getId()
        ) {
          // We don't need to reauth the Firebase connection.
          return true;
        }
      }
    }
    return false;
  };

The main function to save to firebase:

  const onSignIn = async (googleUser: any) => {
    const unsubscribe = firebase.auth().onAuthStateChanged((firebaseUser) => {
      unsubscribe();
      // Check if we are already signed-in Firebase with the correct user.
      if (!isUserEqual(googleUser, firebaseUser)) {
        // Build Firebase credential with the Google ID token.
        const credential = firebase.auth.GoogleAuthProvider.credential(
          googleUser.getAuthResponse().id_token
        );
        // Sign in with credential from the Google user.
        firebase
          .auth()
          .signInWithCredential(credential)
          .then((res: any) => {
            if (res.additionalUserInfo.isNewUser) {
              firebase.database().ref(`/users/${res.user.uid}`).set({
                email: res.user.email,
                display_name: res.user.displayName,
                created_at: Date.now(),
                profile_picture: res.additionalUserInfo.profile.picture,
                first_name: res.additionalUserInfo.profile.given_name,
                last_name: res.additionalUserInfo.profile.family_name,
                login_with: 'google',
              });
            } else {
              firebase.database().ref(`/users/${res.user.uid}`).update({
                last_logged_in: Date.now(),
              });
            }
          })
          .catch((error: any) => {
            // Handle Errors here.
            const errorCode = error.code;
            const errorMessage = error.message;
            // The email of the user's account used.
            const email = error.email;
            // The firebase.auth.AuthCredential type that was used.
            const credential = error.credential;
          });
      } else {
        console.log('User already signed-in Firebase.');
      }
    });
  };

Its pretty hard to debug because it is not available on emulator or device on development, since its required reversed_id upon building the app. I am not planning to use expo-google-app-auth since I prefer this approach more. Does anyone face similar issue? Please help. Thank you.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.