Unity/Firebase How to authenticate using Google?

The simple answer is that there's no way in the Firebase SDK plugin for Unity to do the entirety of Google's authentication in a Unity app. I would recommend following the instructions for email/password sign on to start out. https://firebase.google.com/docs/auth/unity/start

If you really want Google sign on (which you probably do for a shipping title), this sample should walk you through it: https://github.com/googlesamples/google-signin-unity

The key is to get the id token from Google (which is a separate step from the Firebase plugin), then pass that in.

I hope that helps (even if it wasn't timely)!


Here is the entirety of my Google SignIn code w/ Firebase Authentication and GoogleSignIn libraries:

private void SignInWithGoogle(bool linkWithCurrentAnonUser)
   {
      GoogleSignIn.Configuration = new GoogleSignInConfiguration
      {
         RequestIdToken = true,
         // Copy this value from the google-service.json file.
         // oauth_client with type == 3
         WebClientId = "[YOUR API CLIENT ID HERE].apps.googleusercontent.com"
      };

      Task<GoogleSignInUser> signIn = GoogleSignIn.DefaultInstance.SignIn();

      TaskCompletionSource<FirebaseUser> signInCompleted = new TaskCompletionSource<FirebaseUser>();
      signIn.ContinueWith(task =>
      {
         if (task.IsCanceled)
         {
            signInCompleted.SetCanceled();
         }
         else if (task.IsFaulted)
         {
            signInCompleted.SetException(task.Exception);
         }
         else
         {
            Credential credential = Firebase.Auth.GoogleAuthProvider.GetCredential(((Task<GoogleSignInUser>)task).Result.IdToken, null);
            if (linkWithCurrentAnonUser)
            {
               mAuth.CurrentUser.LinkWithCredentialAsync(credential).ContinueWith(HandleLoginResult);
            }
            else
            {
               SignInWithCredential(credential);
            }
         }
      });
   }

The parameter is for signing in with intentions of linking the new google account with an anonymous user that is currently logged on. You can ignore those parts of the method if desired. Also note all of this is called after proper initialization of the Firebase Auth libraries.

I used the following libraries for GoogleSignIn: https://github.com/googlesamples/google-signin-unity

The readme page from that link will take you through step-by-step instructions for getting this setup for your environment. After following those and using the code above, I have this working on both android and iOS.

Here is the SignInWithCredential method used in the code above:

private void SignInWithCredential(Credential credential)
   {
      if (mAuth != null)
      {
         mAuth.SignInWithCredentialAsync(credential).ContinueWith(HandleLoginResult);
      }
   }

mAuth is a reference to FirebaseAuth:

mAuth = Firebase.Auth.FirebaseAuth.DefaultInstance;