简体   繁体   中英

How to use the same Anonymous user on Firebase?

I want a guest user to login, and create only 1 new user on my auth db.

Problem-

  • -On Unity editor- A new anonymous user is created with each launch of the app
  • On Android the guest user stays logged in but when the app is uninstalled- then another new anonymous user is created

My db is just full of anonymous users.

Note- I do use- and check monitoring authentication state

How can I use the same Anonymous user? I was even thinking about login with a custom token, and save it on the user's device- but then I need to create my own uid on the admin side and not sure if it's reliable or even a good solution?

It seems like Firebase doesn't give a proper solution for that. You'd think it's basic. Am I missing something?

Code for unity-

void InitializeFirebase()
{
    Debug.Log("Setting up Firebase Auth");

    auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
    auth.StateChanged += AuthStateChanged;
    AuthStateChanged(this, null);
}

void AuthStateChanged(object sender, System.EventArgs eventArgs)
{
    if (auth.CurrentUser != user)
    {
         bool signedIn = user != auth.CurrentUser && auth.CurrentUser != null;
        if (!signedIn && user != null)
        {
            Debug.Log("Signed out " + user.UserId);
        }
        user = auth.CurrentUser;
        if (signedIn)
        {
            Debug.Log("Signed in " + user.UserId);
        }
    }
}

void OnDestroy()
{
    auth.StateChanged -= AuthStateChanged;
    auth = null;
}
   public static void SignAnonymous()
{
  auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
  auth.SignInAnonymouslyAsync().ContinueWith(task => {
  user = task.Result;

        Debug.LogFormat("User signed in successfully: {0} ({1})",
        user.DisplayName, user.UserId);
    });

}

store device id in firebase database and every time you sing in using anonymous sign in method, you can retrieve the same data. i don't think firebase let's us use same anonymous user id to use again and again right now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM