0
\$\begingroup\$

I want to add an auto login/sign in ability into my game, so when the player quits the game or goes to main menu, the login panel is gone and the player is automatically logged in. Then, I want the player to be logged out and no longer automatically sign in when he presses the "sign out" button.

Here are some previews of my game:

I'm sure this code works like this, but I have low experience on firebase. Additionally, there is lack of documentation on it with Unity so I don't know what is wrong with this script.


void Awake()
    {
        //Check that all of the necessary dependencies for Firebase are present on the system
        FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
        {
            dependencyStatus = task.Result;
            if (dependencyStatus == DependencyStatus.Available)
            {
                //If they are avalible Initialize Firebase
                InitializeFirebase();
            }
            else
            {
                Debug.LogError("Could not resolve all Firebase dependencies: " + dependencyStatus);
            }
        });

        //singleton
        if (instance != null)
        {
            Destroy(gameObject);
        }
        else
        {
            instance = this;
            DontDestroyOnLoad(gameObject);

        }

    }

    private void InitializeFirebase()
    {
        Debug.Log("Setting up Firebase Auth");
        //Set the authentication instance object

        if (auth != null)
        {
            _MainMenuUI.instance.loginPanel.SetActive(false);
        }
        else
        {
            auth = FirebaseAuth.DefaultInstance;
            DBreference = FirebaseDatabase.DefaultInstance.RootReference;
        }
    }
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Autologin means you would need to store somewhere the user/ password or token. This would involve storing it somehow and is not related to firebase. Don't forget to set the loginPanel to true if your auth is null in case user logs out and has now no option to login back. \$\endgroup\$
    – Zibelas
    Jan 27, 2021 at 10:26
  • \$\begingroup\$ @Zibelas so i've to store it like on the playerprefs, but why when I go to main menu loginpanel true instead false, I mean I suppose to be cant login because I've login at first and haven't sign out yet. \$\endgroup\$ Jan 27, 2021 at 10:38
  • 1
    \$\begingroup\$ You should not change the true to false but in your else branch you should enable the login panel or at least make sure that somewhere in your code you set it to true to display it again after you logged out. \$\endgroup\$
    – Zibelas
    Jan 27, 2021 at 10:48

2 Answers 2

1
\$\begingroup\$

Taken from the documentation with some added comments to explain. The main difference between this and the other answer is, that it includes the check against user. While that may not be making much of a difference in your app, it could lead to the problem depending on how you store data - are you using the user or the auth.user - if they are not the same, you might persist data under the wrong user.

void AuthStateChanged(object sender, System.EventArgs eventArgs) {
  //This checks if the user (your local user) is the same as the one from the auth
  if (auth.CurrentUser != user) {
    //this seems the same, but user could have been null before
    bool signedIn = user != auth.CurrentUser && auth.CurrentUser != null;
    if (!signedIn && user != null) {
      Debug.Log("Signed out " + user.UserId);
    }
    //this is important step, this user is the one you should be working with
    user = auth.CurrentUser;
    if (signedIn) {
      Debug.Log("Signed in " + user.UserId);
    }
  }
}

//it does not directly log the user out but invalidates the auth
void OnDestroy() {
  auth.StateChanged -= AuthStateChanged;
  auth = null;
}

Another thing is, the AuthStateChanged callback might not be only called on your login screen. You could get reference errors to your panels in case it happens outside of the scene. (Many games have the option to login/ logout from an option setting that might be reachable from anywhere in the game). Instead on your loginscreen you should just check if the user is the same as the auth.user and none of them are null, the same as the bool signedIn but not handling the UI from AuthStateChanged. Start or Awake might be a better place for handling it.

\$\endgroup\$
0
\$\begingroup\$

so i've read some doc the point is the firebase is do auto login

so I've change my script like this, base on doc firebase.


void Start()
    {
       InitializeFirebase()
    }
    

 void InitializeFirebase()
    {
        auth = FirebaseAuth.DefaultInstance;
        DBreference = FirebaseDatabase.DefaultInstance.RootReference;
        auth.StateChanged += AuthStateChanged;
        AuthStateChanged(this, null);
    }

    void AuthStateChanged(object sender, System.EventArgs eventArgs)
    {
        if (auth.CurrentUser != null)
        {
            bool signedIn = auth.CurrentUser != null;
            if (!signedIn)
            {
                Debug.Log("Signed out " + User.UserId);
                loginPanel.SetActive(true);
            }
            User = auth.CurrentUser;
            if (signedIn)
            {
                Debug.Log("Signed in " + User.UserId);
                loginPanel.SetActive(false);
                userNameShowText.text = User.DisplayName;

                //emailAddress = user.Email ?? "";
                //photoUrl = user.PhotoUrl ?? "";
            }
        }
        else
        {
            // haven't login yet
           // your code
           loginPanel.SetActive(true);
        }
```
\$\endgroup\$
5
  • \$\begingroup\$ Login panel. Setactive(true) can never be executed, the part can't be reached \$\endgroup\$
    – Zibelas
    Jan 28, 2021 at 15:56
  • \$\begingroup\$ @Zibelas thank you for mention it, i forgot to call it on start method. \$\endgroup\$ Jan 28, 2021 at 23:40
  • \$\begingroup\$ That's not what I mean. The code logic in AuthStateChanged is wrong. signed In can only be true. \$\endgroup\$
    – Zibelas
    Jan 29, 2021 at 7:09
  • \$\begingroup\$ @Zibelas, but its from the firebase document, could you please show me the AuthstateChanged properly ? \$\endgroup\$ Jan 29, 2021 at 8:08
  • 1
    \$\begingroup\$ According to firebase.google.com/docs/auth/unity/manage-users the first null check should be against user and not null. Your code is flawed in that way that if there is a current user, he is signed in as well (since it is the same null check as before, if the first one is true, the second one is as well.) And that sets the signed in to always true. What the code should do is comparing the user from the system/ game against the logged in user to check if they are the same. \$\endgroup\$
    – Zibelas
    Jan 29, 2021 at 9:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .