Skip to content

[🐛] Android - onAuthStateChanged does not fire when I refresh the app from the metro terminal by pressing R #7819

@mehtaanirudh

Description

@mehtaanirudh

Issue

So auth.onAuthStateChanged() works fine when I open the app in emulator. However, when I press R on the metro terminal the app reloads but auth.onAuthStateChanged() does not fire. It works fine for iOS.

Is this an expected behaviour? I am using react native CLI

My versions are -

"@react-native-firebase/app": "^20.0.0", "@react-native-firebase/auth": "^20.0.0", "react": "18.2.0", "react-native": "0.74.1"

My JS:

import { View, Text } from 'react-native';
import auth from '@react-native-firebase/auth';


function App() {
  // Set an initializing state whilst Firebase connects
  const [initializing, setInitializing] = useState(true);
  const [user, setUser] = useState();

  // Handle user state changes
  async function onAuthStateChanged(user) {
    setUser(user);
    if (initializing) setInitializing(false);
  }


  useEffect(() => {
    const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
    if (auth().currentUser) {
      onAuthStateChanged(auth().currentUser);
    }
    return subscriber; // unsubscribe on unmount
  }, []);

  if (initializing)  return (
    <View>
      <Text>Loading</Text>
    </View>
  );

  if (!user) {
    return (
      <View>
        <Text>Login</Text>
      </View>
    );
  }

  return (
    <View>
      <Text>Welcome {user.email}</Text>
    </View>
  );
}

export default App;

Closing and reopening the app shows Login but when I press R I see Loading only

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions