iosfirebasereact-nativeexporeact-native-firebase

React Native Firebase onAuthStateChanged returns user after signOut


I'm struggling with signing out in my app. I'm using freshly installed managed Expo app with dev-client and @react-natice-firebase/auth, all in their current version. For the user state i'm using Recoil. Sign in works just fine, the problem occurs when signing out. Here is my AuthProvider.ts

import React, { FC, Fragment, useEffect } from 'react';

import auth, { FirebaseAuthTypes } from '@react-native-firebase/auth';
import { useSetRecoilState } from 'recoil';

import { firebaseUser } from '../../../state/user/atoms/firebaseUser';

export const AuthProvider: FC = ({ children }) => {
  const setUser = useSetRecoilState(firebaseUser);
  const onAuthStateChanged = (user: FirebaseAuthTypes.User | null) => setUser(user);

  useEffect(() => {
    const subscriber = auth().onAuthStateChanged(onAuthStateChanged);

    return subscriber;
  }, []);

  return <Fragment children={children} />;
};

And the function I use to sign out:

const logout = () => async () => await auth().signOut()

<MainButton action={logout()} label={'Logout'} />

After sign in the onAuthStateChanged listener returns user object, but after hitting 'Logout' button it returns the user object again instead of null and the interface doesn't update. After hitting it again, console returns an error:

[Unhandled promise rejection: Error: [auth/no-current-user] No user currently signed in.]

onAuthStateChanged is not called again, user state persists and the interface doesn't update.

User state is cleared after restarting the app and the sign in works just fine again. The behaviour is similar when switching users (eg. from anonymous to signed in).


Solution

  • Maybe someone will face the same problem in the future, so here's what was causing it. It wa Recoil, apparently the two libraries don't work well together, so I've rewritten it using context and works just fine.