Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Unity Firebase async/await blocking code.

Discussion in 'Authentication' started by Seforius, Jun 22, 2023.

  1. Seforius

    Seforius

    Joined:
    Jun 25, 2022
    Posts:
    10
    I'm encountering an async/await issue while using Firebase to authenticate a user with SignInWithEmailAndPasswordAsync() in a multiplayer game built using Netcode, Relay, Lobby, and Authentication. Here is the code, which is triggered by the press of a login button:

    if(auth == null) {Debug.Log("auth is null");}

    FirebaseUser AttemptedUser = null;

    //Call the Firebase auth signin function passing the email and password
    try
    {
    Debug.Log("about to try to log in with email and password"); //<----- LINE 1
    AttemptedUser = await auth.SignInWithEmailAndPasswordAsync(_email, _password);
    Debug.Log("finished awaiting SignInWithEmailAndPasswordAsync"); <----- LINE 2
    }
    catch(Exception e)
    {
    Debug.Log(e.GetType());
    Debug.Log(e.Message);
    }
    finally
    {
    Debug.Log("in FAM finally");
    //User is now logged in, so get the result
    User = AttemptedUser;
    Debug.LogFormat("User signed in successfully: {0} ({1})", User.DisplayName, User.Email);
    //change the UI
    warningLoginText.text = "";
    confirmLoginText.text = "Logged In";

    Debug.Log("about to call UnityLogin");
    //log them into Unity
    await UnityLogin(anonymous: false);
    Debug.Log("finished calling UnityLogin");
    }

    Problem: Everything after the await call (Debug.Log("finished awaiting SignInWithEmailAndPasswordAsync"); and onwards into the finally block) does not get called until after I exit the game (by hitting the play button in the editor to stop the game). Specifically, the line which is commented "LINE 1" runs, but "LINE 2" does not run until exiting the game.

    Interestingly, when playing the game after first loading the game, this code runs fine no problem. However, after loading a new scene, joining a Relay allocation and Lobby, and then leaving the Lobby and reloading this menu scene, the await line hangs and suspends the following code in that snippet until I exit the game, so, something to do with loading the other scenes or hosting/joining a relay/lobby must be messing with the await line. Any help is extremely appreciated as I am in a rush to publish this game.

    Thanks!

    - Seforius.