Firebase Auth匿名登录 [英] Firebase Auth Anonymous Login

查看:146
本文介绍了Firebase Auth匿名登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Firebase Auth匿名帐户时,有时会在系统中创建一个新的UserID,有时还会使用相同的UserID.我真的希望它每次都创建相同的UserID,以便匿名用户仍可以在应用程序中维护相同的进度/数据.实际上,这就是我开始使用Firebase的原因.即使重新启动应用程序等后,我如何始终维护一个匿名帐户以保留相同的UserID?

When using Firebase Auth Anonymous Account it occasionally creates a new UserID in the system and sometimes it uses the same UserID. I really want this to create the same UserID everytime so the anonymous user can still maintain the same progress/data in the app. This is actually the reason I started using Firebase was for this feature. How can I always maintain an anonymous account to keep the same UserID even after relaunching the app, etc.?

我希望用户每次作为来宾玩游戏时始终获得相同的ID.我已经看到一些应用程序,即使在卸载/重新安装后仍然存在.用户将在什么情况下获得新的ID?

I want the user to always get the same ID everytime they play as a guest. There is apps that I have seen that even persist after uninstall/reinstall. What are the situations in which the user will get a new ID?

按照建议实施Firebase Auth后,除非我完全卸载应用程序并重新安装,否则它将维护匿名用户ID.然后,不再检测到匿名用户ID,这意味着该用户将不再具有其来宾游戏数据.同样,假设用户以访客身份登录并选择注销(即auth.Signout()),那么他们也将无法再次访问其原始访客游戏数据.我还在这里遗漏了什么吗,还是Firebase Auth达不到我的初衷?

After implementing firebase Auth as suggested it will maintain the Anonymous UserID unless I completely uninstall the app and reinstall. Then the Anonymous UserID is no longer detected which means the user will no longer have their Guest game data. Also, suppose the user is signed in as a guest and chooses to logout (ie. auth.Signout()) then they will also not be able to access their original Guest game data again. Am I still missing something here or does Firebase Auth not achieve my original intentions?

推荐答案

因此,就我而言,我想显示一个具有注册功能的登录屏幕,但也可以显示跳过登录"选项.如果用户多次跳过登录,则我正在执行signInAnonymous,尽管我正在重用该用户,但没有.

So, in my case I wanted to show a login screen with signup but also the option to Skip login. If the user skips the login multiple times, I was doing signInAnonymous which I though it was reusing the user, but no.

所以我这样解决了它:

componentDidMount() {

    this.unsubscriber = firebase.auth().onAuthStateChanged((user) => {
      this.setState({ user: user, loadingUser: false}, () => {
        if (this.state.user != null && this.state.user.isAnonymous == false)
          this.startApp();
      });
    });

  }




skip = () => {
    this.setState({loading: true}, () => {
      if (this.state.user != null && this.state.user.isAnonymous == true)
        this.startApp();
      else {
        firebase.auth().signInAnonymouslyAndRetrieveData().then((result) => {
          this.setState({user: result.user}, () => {
            this.startApp()
          })
        })
        .catch(err => {
          this.setState({loading: false});
          alert(err)
        });
      }

    });

  }

代码是React Native,但它应该可以帮助您了解逻辑.我等待auth状态并临时存储用户.如果不是匿名用户,则启动主屏幕.如果它是匿名的,我给用户注册的选项.如果仍然要跳过,则只需启动该应用程序即可重新使用该ID.

Code is React Native but it should help you see the logic. I wait for the auth state and store the user temporarly. If it is not anonymous I start the home screen. If it is anonymous I give the user the option to register. If it still wants to skip, then I just start the app so I can re use the ID.

这篇关于Firebase Auth匿名登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆