Firebase auth state changed event does not work

I’m using Firebase plugin in my game and i have successfully used authentication, things like creating new user with email and password, signing in and out. There is an event for FirebaseAuth object called StateChanged, and I thought it would be very nice if I subscribe to this event hoping to get it fired every time a user is signed in, signed out or when authentication token expires. The problem is that it never actually fires. I tried to subscribe to the event in an awake method of my UserManager custom script, and also I tried to wrap it inside FirebaseApp.CheckAndFixDependenciesAsync() like so:

private void Awake()
{
    
    FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
        var dependencyStatus = task.Result;
        if (dependencyStatus == DependencyStatus.Available)
        {
            // Create and hold a reference to your FirebaseApp, i.e.
            //   app = Firebase.FirebaseApp.DefaultInstance;
            // where app is a Firebase.FirebaseApp property of your application class.

            // Set a flag here indicating that Firebase is ready to use by your
            // application.
            auth.StateChanged += AuthStateChanged;
            Debug.Log("Firebase is ready");
        }
        else
        {
            Debug.LogError(System.String.Format(
                "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
            // Firebase Unity SDK is not safe to use here.
        }
    });
}

I can see Debug.Log("Firebase is ready");

Here is an event handler:

void AuthStateChanged(object sender, System.EventArgs eventArgs)
{
    Debug.Log("Auth state changed");
}

And it is never fired up.

Same for me, only with email/password auth on Unity. did you make progress?

This is likely not the problem you had but it might be someone else’s so I’m sharing just in case. After implementing Auth in my app I was missing prints from the code that I was expecting to be executed.


So I wen back to the firebase quick start sample for Auth and I added a “google sing in” button and a “firebase sign in using google token” button to it. I made it use the same firebase setup, keys, and json as my app and I rand them side by side to compare prints. In my app I am subscribing to unity’s built-in Debug.Log to display the output in-app.


Right away I noticed that some of the callbacks didn’t seem to be triggered in my App because the prints inside the functions body were missing, however they were being logged as expected from the sample app. My StateChanged and IdTokenChanged callbacks weren’t printing their respective logs, and the initial CheckAndFixDependenciesAsync call wasn’t printing the logs inside its body (when task completed).


Well it turns out that Debug.Log wasn’t working as I expected but it was still my fault: For it to work correctly I had to register NOT to Application.logMessageReceived but to Application.logMessageReceivedThreaded instead because the calls to Debug.Log were being called from a different thread (inside a task).


Keep in mind that unity API calls have to be called fro the main thread only or very likely your app will start crashing or failing in strange ways. Also make sure that your handle for Application.logMessageReceivedThreaded is thread safe: Example