React Native – Firebase auth持久性不起作用

Firebase身份验证不会持续登录用户,每次刷新或重新打开应用程序时我都必须重新登录.

我已经尝试将持久性设置为本地,并且回调确实验证了它的设置,但持久性仍然无效

为了设置持久性我正在使用…

//set auth persistence
  firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
    .then(function() {
      console.log("successfully set the persistence");

    })
    .catch(function(error){
    console.log("Failed to ser persistence: " + error.message)
  });

.
.
.
登录时我正在使用此代码

firebase.auth().signInWithEmailAndPassword(email,password)
      .then((user) =>{
        this.checkAccountStatus(user.uid,user.email);
      })
      .catch(function(error) {
      // Handle Errors here.

      var errorCode = error.code;
      var errorMessage = error.message;

      console.log(errorMessage)
      // ...
    });

这是我用来检查登录状态的代码……

if (firebase.auth().currentUser) {
        const currentUser = firebase.auth().currentUser;
        console.log("Signed in username" + currentUser.displayName);

        this.props.navigation.navigate('AppTab');
      }else{
        console.log("no user signed in");
        this.props.navigation.navigate('AuthTab');
      }

如果有什么我做得不对

您不需要设置持久性. Firebase认为您处理.您只需要调用函数来检查用户是否已记录:
firebase.auth().onAuthStateChanged((user) => {
      if (user) {
        console.log('user is logged');
      }
}

仅当用户已注销或清除应用数据时,才会触发此操作.

您可以在官方文档中找到更多详细信息:https://firebase.google.com/docs/auth/web/manage-users

希望能帮助到你.

相关文章

react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...
react 本身提供了克隆组件的方法,但是平时开发中可能很少使...
mobx 是一个简单可扩展的状态管理库,中文官网链接。小编在接...
我们在平常的开发中不可避免的会有很多列表渲染逻辑,在 pc ...