react native Firebase onAuthStateChanged does not get current user after closing the app while the user its authenticated using social media providers

Viewed 227

hello we have the following problem using firebase with react native and social login providers.

The login is not persistent after a successful login using the three major social login providers (google, apple, facebook). The function onAuthStateChanged does not return the user after closing the app while the user logged in using the former providers, therefore the user has to login again every time the app is opened. This problem is not present while using auth with email and password credentials when the app persist user after login and closing the app.

we are using the following technogologies and versions

  • react-native: 0.63.3
  • firebase: 8.10.1 (previously we were using 7.8.2 )
  • react-native-firebase/app": "^14.2.2",
  • "@react-native-firebase/auth": "^14.2.2"

for creating the credentials we are using the following packages:

  • @react-native-google-signin/google-signin": "^7.0.3"
  • @invertase/react-native-apple-authentication": "^2.1.5"
  • react-native-fbsdk-next": "^6.2.0 -> facebook login

we don't know if it's a problem with firebase because the listener function does not get the user after closing the app with a successful login.

I hope someone can helps us out.

Edit: here is example code how we are using the firebabse and the libraries to login using apple:

on App.js, we have the auth listener

    auth.onAuthStateChanged(async (user) => {
      try {
        if (user) {
          const userInfo = await getUserById(user.uid);
          const userFetched = userInfo.val();

          setUser(userFetched);
        }
      } catch (error) {
        if (error.message !== 'user is null') {
          console.log('ERROR: ' + error.message);
        }
      }
    });

login the user and creating the credentials fo

const onAppleButtonPress = async() => {
    if (Platform.OS === 'android'){
      return onAppleButtonPressAndroid;
    }
    try {
      const appleAuthRequestResponse = await appleAuth.performRequest({
        requestedOperation: appleAuth.Operation.LOGIN,
        requestedScopes: [appleAuth.Scope.EMAIL, appleAuth.Scope.FULL_NAME],
      });
      const {
        user: newUser,
        email,
        nonce,
        identityToken,
        realUserStatus /* etc */,
      } = appleAuthRequestResponse;

      if (!identityToken){
        Toast.show({
          type: 'error',
          text1:'error al ingresar con las credenciales',
        });
        return;

      }

      // loguear con firebase here
      const response = await SignInWithCredentialApple(identityToken,nonce);
      const usermail = response.user.email;
      const userFound = await searchUserByEmail(usermail);
      if (!userFound.exists()){
        const newUser = {
          uid: response.user.uid,
          email: usermail,
          role: 'user',
          userName: usermail.split('@')[0],
          profileFilled: false,
          created: new Date(),
          authMethod: 'apple',
        };
        setUser(newUser);
        //setUser(newUser);
        await addUser(newUser.uid, newUser);
      } else {
        setUser(Object.values(userFound.val())[0]);
      }
    } catch (error){
      setShowError(true);
      if (error.code === appleAuth.Error.CANCELED) {
        console.warn('User canceled Apple Sign in.');
        setError('Operacion cancelada por el usuario');
      } else {
        setError(`Error:  ${error.toString()}`);
      }

    }


   };

login and creating the credentials

import authRNFirebase from '@react-native-firebase/auth';

export const SignInWithCredentialApple = async (identityToken, nounce) => {
  //return auth.signInWithCredential(credential);

  const credential = authRNFirebase.AppleAuthProvider.credential(
    identityToken,
    nounce,
  );

  return authRNFirebase().signInWithCredential(credential);
};
0 Answers
Related