【问题标题】:React Native - Firebase auth persistence not workingReact Native - Firebase 身份验证持久性不起作用
【发布时间】:2018-02-18 23:05:00
【问题描述】:

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”包......“firebase”:“^4.3.0”,

标签: firebase react-native firebase-authentication persistence


【解决方案1】:

Firebase 现在建议使用firestore 而不是realtime-database,它默认管理离线持久性。在其文档中很清楚here

你只需要通过这段代码访问用户:

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

如果用户已登录,即使您关闭应用程序,此代码也会将您带到主屏幕。要清除用户的凭据,您需要使用此代码手动注销用户。

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

您还可以查看this 简单应用程序(在其中使用react hooks)以获得进一步的帮助。

【讨论】:

    【解决方案2】:

    您不需要设置持久性。 Firebase 默认为您处理。你只需要调用这个函数来检查用户是否登录:

    firebase.auth().onAuthStateChanged((user) => {
          if (user) {
            console.log('user is logged');
          }
    });
    

    只有在用户退出或清理应用数据时才会触发。

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

    希望对你有帮助。

    【讨论】:

    • 我正在使用这种方法... firebase.auth().currentUser;...一旦应用程序使用 Cmd+R 刷新或从模拟器本身重新启动,它就不起作用
    • 您应该将您的方法替换为我提供的方法。根据文档,这是在应用程序关闭后检查用户是否登录的正确方法。让我知道是否有效。
    • 刚刚试了一下,它没有用,我也不期待它,因为根据文档,如果用户已经签名是 .currentUser() 是知道状态是否已登录的正确方法或退出....请参阅标题“获取当前登录的用户”firebase.google.com/docs/auth/web/manage-users
    • 我正在工作的当前项目使用 Firebase 4.1.3 和 RN 0.42.3
    • @soutot 的回答是一种更好的方法。这里的官方文档说:注意:currentUser 也可能为 null,因为 auth 对象尚未完成初始化。如果您使用观察者来跟踪用户的登录状态,则无需处理这种情况。 firebase.google.com/docs/auth/web/manage-users
    【解决方案3】:

    确保您没有使用您正在使用的 API 密钥限制 console 中的“令牌服务 API”。我没有将服务添加到我的密钥中,即使使用上面建议的正确代码,它也会每 3-4 小时将我注销一次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-18
      • 2019-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      • 2019-09-20
      • 2019-05-23
      相关资源
      最近更新 更多