Skip to content Skip to sidebar Skip to footer

React-native - Firebase Onauthstatechanged Not Working Correctly

I have not been able to persist a firebase session in my react-native application. This question is nearly identical to React Native - Firebase auth persistence not working but th

Solution 1:

It's been a while since I posted this question but I thought I would post my solution.

As it turns out my initial set up (kind of) worked. By that I mean that onAuthStateChanged was taking some time to hear back from firebase and during that time the rest of the application loaded thinking that the user was not logged in, due to that being the default state.

I managed to get around this by moving the onAuthStateChanged logic out of the app.js and in to the initial home page in componentWillMount. which now looks like this:

componentWillMount() {
const { navigate } = this.props.navigation;
firebase.auth().onAuthStateChanged((user) => {
  if (user) {

    SplashScreen.hide()
    this.setState({ loading: false })

    this.props.persistedUserLoginSuccess({user, navigate})

    this.props.fetchUserData();

  } else {

    SplashScreen.hide()
    this.setState({ loading: false })

  }
});

Also, I managed to better the user experience by forcing the splash screen to stay up which the auth was being queried. Upon completion the splash screen was hidden and the application loaded as either a logged in application or one where the user had not yet logged in.

Solution 2:

This issue is because you are not doing Firebase.auth().signOut() after once loggedIn. Try to signOut (for testing you can always sign out in componentWillMount function) and you will be able to see the onAuthStateChanged notification.

Post a Comment for "React-native - Firebase Onauthstatechanged Not Working Correctly"