Search code examples
firebaseunity-game-enginefirebase-authenticationgoogle-signinfacebook-login

How exactly do I implement the Google & Facebook Sign-In?


For the last couple of days, I have faced an issue with my Unity project regarding adding Google and Facebook authentication for my users, using Firebase. The thing is that there exist a couple of youtube tutorials for each of those two implementations but they are quite misleading and rushed.

Apart from that those tutorials refer to the creation of a new project which implies that the Firebase Auth package is not yet installed. And I mention this because I've seen plenty of people in the comments that are struggling with the Plugins and Parse folders that get overwritten whenever both Firebase Auth and Google or Facebook packages are installed. So there does not exist a rule of thumb on how to proceed in cases where projects already had other methods of authentication such as Email/Password.

Of course, the documentation for both Google and Facebook is unclear on how to get the access token.

Facebook SignIn doc: https://firebase.google.com/docs/auth/unity/facebook-login?hl=en&authuser=0

Google SignIn doc: https://firebase.google.com/docs/auth/unity/google-signin?hl=en&authuser=0

I've made this post in order to find a step-by-step solution to my problem and hopefully help the community find a solution to a problem that concerns many developers.

[Edit]: My question is. How exactly do I implement the Google & Facebook Sign-In, based on the documentation, that is unclear, especially on the step

  1. Follow instructions for Android and iOS+ to get an access token for the signed-in Facebook user.

And I am speaking for a project that already has imported Firebase.Authentication, which means that folders such as Plugins and Parse are already in the Assets and get overloaded from the packages requested by the 'tutorials' (Because the docs do not even mention what package to use and how).

[Edit]: As of right now I've managed to set up the code in a way that has no errors. When I run it though I get:

[Android]: Exception: Field currentActivity or type signature not found.

[iOS]: EntryPointNotFoundException: GoogleSignIn_Create

After some googling, they proposed testing it after I build it which I think is way too much time-consuming.


Solution

  • This is the answer I've got from Firebase support.

    First of all, we need to note that Firebase only has three native authentication providers: email, phone, and anonymous authentication. For the other methods, Firebase needs to trust how their respective third-party providers handle their authentication flow. That's why Firebase can't offer a way to get the access token. This is not its responsibility.

    So, to sign in with a Federated provider (FP) you must follow the following steps.

    Think about which will be your target mobile platform. Not all the FP provide the same authentication flow on iOS or Android. For example Google offers a more handy way to authenticate Android devices by using Google Play Game Services Authenticate in Unity Using Google Play Games Services | Firebase

    Search on his FP's official GitHub repository for the official plugins to get access to the FP's SDK on Unity. For example, Google offer a useful plugin to authenticate user in both platforms GitHub - googlesamples/google-signin-unity: Google Sign-In API plugin for Unity game engine. Works with Android and iOS. Last plugin's release: https://github.com/googlesamples/google-signin-unity/releases/tag/v1.0.4 Examples - Unity SDK - Documentation - Meta for Developers

    Create the FP credentials according to its own requirements. For example, Google login requires the use of web_client_id, which can be obtained from the google-services.json or GoogleService-Info.plist files needed to build the application on the target platform. https://github.com/googlesamples/google-signin-unity#get-a-google-sign-in-configuration-file https://github.com/googlesamples/google-signin-unity#using-this-plugin-with-firebase-auth

    Once you've built the FP's credential into your project, it's time to check out our Firebase documentation for using the FP credential in our authentication flow. I recommend having the Firebase reference along with the documentation as it will help to know what arguments the required method needs.

    The best way to test your new authentication provider is by implementing the Firebase Authentication Unity Quickstart. Please clone the Unity Quickstart repository here and deploy the Unity project found in the auth folder. Follow the steps mentioned in the README.md file and build the app on your target platform.

    I recommend that you add the FP's SDK right in the same step as adding the Firebase SDK plugins to avoid some build issues. Since these errors are more related to Unity than Firebase, I recommend reaching out to our communities on StackOverflow, Google Groups, and Slack for help with these issues as they have more knowledge than I on those topics.

    So, in conclusion, getting credentials from third-party providers is a task delegated to them and not to Firebase since this one is not responsible for any third-party authentication flow.