React Native - Firebase auth persistence not working

前端 未结 3 1877
你的背包
你的背包 2020-12-25 15:37

Firebase auth does not persist logged in user and everytime I refresh or reopen app I have to sign in again.

I have tried setting persistence to local and the callba

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-25 16:08

    Firebase now recommends to use firestore instead of realtime-database and it manages offline persistence by default. It is clear in its documentation here

    You just need to access the user through this code:

    if (auth().currentUser !== null) {
          console.log('User is logged in');
          console.log(auth().currentUser.email);
          props.navigation.navigate('Home');
        } else {
          props.navigation.navigate('Login');
    }
    

    This code will prop you to home screen if user is logged-in, even when you close the app. To clear the user's credentials, you manually need to sign-out the user using this code.

    try {
          auth()
            .signOut()
            .then(props.navigation.navigate('Login'));
        } catch (error) {
          Alert.alert('Error', error.toString());
    }
    

    You can also check this simple app (used react hooks in it) for further assistance.

提交回复
热议问题