user2455808
user2455808

Reputation: 91

Unity+Firebase hangs from user.TokenAsync

Below is my main login block in unity for firebase. It works fine when i get rid of:
Debug.Log(user.TokenAsync(false).Result);
However due to security i MUST have a way to handle the session. Firebase even recommends this is the way you do it. No matter what you do though false OR true the application hangs and never continues. Am i using the tokenasync incorrectly?
I have also tried using whats commented out:
//StatusText.text = FirebaseAuth.DefaultInstance.CurrentUser.TokenAsync(true).Result;
but the currentuser returns null for some reason

FirebaseUser InvalidVerificationUser;
public void OnLoginButtonPressed()
{
    LoginButton.interactable = false;
    StatusText.text = "Logging in, please wait...";
    FirebaseAuth.DefaultInstance.SignInWithEmailAndPasswordAsync(Email.text, Password.text).ContinueWith((obj) =>
    {
        if (obj.IsFaulted || obj.IsCanceled)
        {
            StatusText.text = obj.Exception.InnerExceptions[0].ToString().Substring(28);
            LoginButton.interactable = true;
            return;
        }
        else if (!obj.Result.IsEmailVerified)
        {
            StatusText.text = "You must verify your email before logging in";
            ResendObject.SetActive(true);
            InvalidVerificationUser = obj.Result;
            LoginButton.interactable = true;
            return;
        }

        FirebaseUser user = obj.Result;
        if (user.DisplayName == "")
        {
            StatusText.text = "CRITICAL: No Username Found!";
        }
        else
            StatusText.text = "Hello: " + user.DisplayName;
        LoginButton.interactable = true;
        Debug.Log(user.TokenAsync(false).Result);
    });
    //StatusText.text = FirebaseAuth.DefaultInstance.CurrentUser.TokenAsync(true).Result;
}

Upvotes: 0

Views: 1791

Answers (2)

Thaina Yu
Thaina Yu

Reputation: 1512

The point is TokenAsync is API that return Task

And so, everyone, don't ever use .Result of the Task object directly, this is common practice. Any task not just firebase api. Especially in unity. It has tendency to freeze the application with deadlock

Use async/await or task.ContinueWith(callBack) instead

Upvotes: 0

Majdie Gideir
Majdie Gideir

Reputation: 166

I was having the same problem with SignInAnonymouslyAsync hanging the Unity client and I got around it by awaiting both SignInAnonymouslyAsync and TokenAsync.

Something like this might help you:

FirebaseUser firebaseUser = await AuthDefaultInstance.SignInWithEmailAndPasswordAsync(Email.text, Password.text);

if (!firebaseUser.IsEmailVerified)
{
    // do stuff here
}
else if (firebaseUser.DisplayName == string.Empty)
{
    // do stuff here
}
else
{
    string token = await firebaseUser.TokenAsync(true);
}

Upvotes: 2

Related Questions