- Home /
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.
Answer by gdwf · Nov 06, 2018 at 07:54 AM
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
Answer by zakmackraken · Oct 02, 2018 at 04:56 PM
Same for me, only with email/password auth on Unity. did you make progress?
It actually works... But not when you logout, and this is where I wanted it to be useful, but it's not. It fires up only on login... and maybe when the token is expired, but i didn't test it yet...
Your answer
![](https://koobas.hobune.stream/wayback/20220612175048im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Breakpoints in MonoBehaviour code called from SmartFoxServer2X.dll cause Editor debugging hangs. 0 Answers
Android obfuscator 1 Answer
Delegate an event 2 Answers