29
votes

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.?

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?

Edit: 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?

3
Anonymous authentication tokens do not expire and are persisted to disk between runs of your app. You will only get a new UID if your app calls signInAnonymously() multiple times. But it's hard to be sure without seeing the minimal code that reproduces the problem. - Frank van Puffelen
Thanks Frank. I updated my question with how I've been signing in guest. It may be my fault in tests. I just want to make sure it should behave as I expected. - Coop
If you call signInAnonymously() you're signing in a new anonymous user. You should only do that when there is no currently signed in user. What platform are you using btw? - Frank van Puffelen
Maybe I'm not understanding this correctly. I can't have a user maintain an anonymous userid throughout every launch of the application? Without storing anything on their local device. Other applications are able to do this so that the guest account will always maintain it's data... - Coop
You can have a user maintain their login. It happens automatically. But every time you call SignInAnonymouslyAsync, you're creating and signing in with a new anonymous user account. To pick up the existing anonymous user's info, you should monitor their authentication state (link in previous comment). - Frank van Puffelen

3 Answers

4
votes

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.

So I solved it like this:

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)
        });
      }

    });

  }

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.

4
votes

You can use the Custom Auth System. Since the "Play as Guest" will only be specified for a special device. You can use Device ID as the CustomToken and then call Firebase Authentication Method:

auth.SignInWithCustomTokenAsync(custom_token).ContinueWith{.......

You can provide "custom_token" as the Device ID. You can get Device ID by:

string deviceID = SystemInfo.deviceUniqueIdentifier;

Hope this helps...

3
votes

When you call SignOut, your guest will signout forever. If you want to keep your guest, don't call SignOut method.