Getting started with Firebase Authentication in Unity - Firecasts
Why Stack Overflow recommends it
Stack Overflow users link it when answering Firebase Auth in Unity questions, especially about enabling email sign-in, waiting on async tasks in coroutines, and keeping auth calls on Unity's main thread.
Recommended by 3 people across 5 different months, 2019 to 2021.
What it is
An 11-minute Firecasts episode from the official Firebase channel in which Patrick Martin shows how to set up Firebase Authentication in a Unity game. It covers the basics of giving players an identity, including email sign-in and handling async calls in Unity.
What people said
Recommends Iggy · 2019-12 · Stack Overflow · on “GameObject.SetActive() inside Firebase Authentication not called”
Most of the Unity Engine API is not thread-safe. The ContinueWith is running on a different thread, and so you can not set Obj.SetActive(true) You can do what Patrick Martin from Firebase recommends [this video] : IEnumerator RegisterUser(string email, string password) { var auth = FirebaseAuth.DefaultInstance; var registerTask = auth.CreateUserWithEmailAndPasswordAsync(email, password); yield return new WaitUntil(() => registerTask.IsCompleted); if (registerTask.Exception != null) { Debug.LogWarning($"Failed to register task with {registerTask.Exception}"); Obj.SetActive(true); } else { Debug.Log($"Successfully registered user {registerTask.Result.Email}"); } } This way you wait in the main thre
Recommends Patrick Martin · 2020-01 · Stack Overflow · on “unity Firebase auth state”
I have good news, there is a sample [LINK] and a video tutorial [this video] ! With that said, let me go through a quick impromptu code review: You are registering the event correctly, and even unregistering in OnDestroy (I find that many folk who are used to UnityEvent will omit this step). I don't think the check against player is doing what you want though. I would make two changes: First, I'd change ContinueWith to ContinueWithOnMainThread . If you ever end up making any calls into the UnityEngine namespace other than Debug.Log , you're asking for trouble. Next, FirebaseUser is implemented in sort of an odd way right now. It references a global static object on the C++ side rather than being
Recommends Patrick Martin · 2020-01 · Stack Overflow · on “Firebase unity android app authentication not working”
I can't say that this is the issue, but in RegisterUser you're checking for task.IsCompleted . This will always be true whether or not the task failed or succeeded (I often use yield return new WaitUntil(()=>task.IsCompleted); to wait for async calls in Unity coroutines). I suspect that there's a chance that you may not have: Enabled email and password: [LINK] Downloaded your google-services.json [LINK] file. Or uploaded your SHA-1 [LINK] . Further, if you're testing on mobile, I've definitely had issues with mobile keyboards adding an extra space to your password (especially if you haven't configured the input type). If you've done everything else I listed above, try deleting one character
Recommends Patrick Martin · 2020-04 · Stack Overflow · on “My unity login with Firebase is not working”
Replace ContinueWith with ContinueWithOnMainThread , I think you should be good. If not, make sure you've enabled email sign in in the Firebase console. I suspect that everything's working as expected, but because you're using ContinueWith your SceneManager logic is happening off of the main thread. In the best case scenario, this just won't work. In the worst case, you might see a crash. See my article on threading [LINK] in Unity and my video on Authentication [this video] for more information.
Recommends Patrick Martin · 2020-12 · Stack Overflow · on “How can I authenticate with Google Play Services when using Realtime Database's API?”
First I'll apologize by not answering the question directly but by trying to answer the spirit of the question. Since you are using Realtime Database and Firebase Authentication, it's worth pointing out that as long as you're building a mobile game (iOS/Android) you're best off using the official Firebase SDK [LINK] . Since desktop support is beta, other platforms aren't there (consoles, web, &c), and some devs prefer not to use native plugins feel free to vote the answer down due to this initial caveat. With that out of the way, first I'll point out that there is an example online leaderboard [LINK] you can pull directly into your project that uses the Realtime Database Unity SDK. I'd recom
Recommends Onat Korucu · 2021-05 · Stack Overflow · on “Firebase Auth failed on iOS (Unity, Xcode)”
I was seeing "CreateUserWithEmailAndPasswordAsync encountered an error: System.AggregateException: An internal error has occurred." in the Unity console. To fix, I closed Unity, committed my important changes, and re-opened Unity. In the pop-up that appeared, I clicked "Enable". [LINK] Meanwhile, I also went to my project settings in the Firebase console ( [LINK] ) and enabled mail signup. It is also advised in the video tutorial, but not in the written one. Firebase Official Video Tutorial [this video] After that, problem got solved.
Cites Patrick Martin · 2019-11 · Stack Overflow · on “Authentication Race Condition Causes New Users To Trip Security Rules”
It doesn't sound like this is an issue based on the question, but the easiest suggestion I have is to make sure you have anonymous authentication enabled: [LINK] Next, you can check to see if the user is currently authenticated by checking: FirebaseAuth.DefaultInstance.CurrentUser != null; If you wanted, you could also check IsAnonymous [LINK] on CurrentUser. I like to check this in a handler to the StateChanged [LINK] listener for a global "the user has signed in" signal. With all that said, since you're using await , you can always say: FirebaseUser user = FirebaseAuth.DefaultInstance.SignInAnonymouslyAsync(); if (user != null) {} or in a coroutine (my favorite when working with Unity): Ta
Cites Patrick Martin · 2019-12 · Stack Overflow · on “Unity 2019.2.12f1 PlayerPrefs halting script”
If you don't mind, I can expand on your answer a little bit. If you use ContinueWithOnMainThread [LINK] instead of ContinueWith, your continuation logic automatically occurs on the main thread (so you can write PlayerPrefs ). I cover a whole ton of ways to handle threading with Firebase and Unity in this article [LINK] if you'd like to compare them. Furthermore, this is a stab in the dark, but if you're using Realtime Database then you may also be using Firebase Authentication [LINK] . If this is the case, then FirebaseAuth.DefaultInstance.CurrentUser [LINK] actually persists between runs of your game (I cover this a little bit in this video [this video] ). Since Realtime Database also caches dat
Cites Patrick Martin · 2020-06 · Stack Overflow · on “how to save firebase auth between scenes”
I think that there might be some confusion here as I see a reference to the official Firebase Unity SDK [LINK] mixed with raw REST calls in RestClient [LINK] . I'll answer this assuming that you're ok with using the Unity SDK [LINK] . This is a far simpler integration than attempting to manually use the Firebase SDK (and gets you nice benefits - such as local caching). 1) Firebase Authentication Once Firebase Authentication is initialized, FirebaseAuth.DefaultInstance.CurrentUser will always contain your currently signed in user or null if the user is not signed in. This value is actually stored in C++ and accessed through C#, meaning that it doesn't actually know about or abide by Unity's t
Cites Nicokam · 2020-12 · Stack Overflow · on “Firebase SDK Auth Package Trouble with Automatically Loading Scene Once Authenticated Unity”
I am stuck with a functionality of the Firebase SDK (Auth package) regarding the Scenes and it's integration. Here's how they work: 1st: Loading Scene Here, I just added the FirebaseInit code EXACTLY as suggested by Patrick, which it's only function is to call the next scene (Login/Register) once everything loads correctly. 2nd: Login/Register Scene Here I do all the Login AND ALSO the register logic. I set up a button that alternates between the two (Activating the respective parent gameObject within the Canvas). Once the user log's in, the 3rd scene comes into play. 3rd: App's Main Screen Scene Main Screen of the app, where the user can LOGOUT and return to the Login Scene. Problem I added