Firebase authentication – Google

Reading Time: 6 minutes
In our app we can register and login using email, and Facebook. For the email authentication we need to provide a name and password. In case of Facebook we can sign-in only by clicking on the Facebook Button without any password. Similarly we can do this using Google authentication.

In this chapter we are going to talk about how we can register and login using the Google authentication.


If you didn’t do the previous chapters, then from GitHub you can download the starter project for this chapter.

GitHub – facebook branch

Step 1 – Enable Google sing-in method in Firebase

As the first step of this chapter, we have to enable the Google sign-in method for our project in Firebase. 

Enable Google sing-in

So open your Firebase account, where you have the project for this app.

There click in the left sidebar on the “Authentication”. In the new window, in the top tab navigation select the “Sing-in method” option. Here you will see all of the possible methods, what you can use for the Firebase authentication.

Here click on the pen icon at the end of the Google’s line.

If you haven’t done the chapter of the “Reset password”, then in this window first you should provide the name of the project, then a support email address.

If you did this chapter before, then you should just click on the “Enable” toggle button.

Click “Save”.

Provide SHA-1 code

If you have done the chapter of the Facebook authentication, then this step will be familier for you, because we will add for the Firebase’s project the SHA-1 code of our app.

First open the app in Android Studio! Then from the right sidebar click on the “Gradle” tab. After that expand the project, then “app”, “Task”, “android” and finally open “signingReport”.

This will open at the bottom of Android Studio a new window and it will generate same information about our project. Here you can find the SHA-1 code, copy it.

Now, in Firebase open the settings of the project.

In this window scroll down to the “Your apps” part, because here will be the field, where we have to provid the SHA-1 code.

If you have done the creation of the Firebase’s project using the Android Studio’s Firebase plugin, then the SHA-1 code gonna be already here.

If it isn’t provided, then click on the “Add fingerprint” button. Copy there the code from Android Studio, then click on the “Save” button.

Before we continue, we have to set back Android Studio from the “signingReport” to “app”, because you will be surprised, why you can’t run the app. 😊

So in Android Studio, at the top menu line, where we have the green “Run” triangle button also, select from the dropdown box the “App” option.

Step 2 – Add the dependencies

The next step is to add the needed dependencies to our project. To do this, first open in Android Studio from the left “Project” tab the “Gradle Scripts”. Here open the “Module build.gradle”.

At the top of the Gradle file we have some lines beginning with the “apply” keyword. If you don’t have here this line, then add it.

apply plugin: ‘’

Then scroll down to the “dependencies {}” part. Here we have all of the implemented dependencies. Here add this dependency from Google.

implementation “$google_auth”

Now, if you open the “Project build.gradle” file, then at the end of it you will see the “ext {}” part, where we have all of the version for the dependencies. If you don’t have there the version to the Google Play Services, then add it there also.

google_auth = ‘17.0.0’

Finally, click in the top right corner on the “Sync Now” link and wait until the build gonna be finished.

Step 3 – The FirebaseViewModel

In the previous chapter we started to implement something new always from the UserRespository::interface. For the Google authentication we won’t need any new methods there. So we are going to use the already implemented functions and we will add 2 new member variables to the FirebaseViewModel::class.

Member variables

The first one will create an instance about the GoogleSignInClient::class. This will provide for us later the client for the sign in.

private lateinit var googleSingInClient: GoogleSignInClient

The second one will be an unmutable variable. We will use this, when the activity returns from the Google sing-in.

private val RC_SIGN_IN = 1


The authentication process will be started in this method, which needs an Activity as the only argument. Later on you will see, why.


Here we are going to show for the user a ProgressBar during the authentication process. Because of this, the code will in side of the launchDataLoad{} method.

Then we are going to create an instance about the GoogleSignInOptions::class, which will get the value from it’s Builder.

Using googleSignInOptions variable, we will request the id of the token. It is already provided by Firebase’s autogeneration.

Then we will tell for the Google sing-in options, that for the registration we need an email. Then we can build it.

In the next line, we will get the client from Google using the activity and the googleSignInOptions variables.

In the last 2 lines we gonna start an Intent, which is the signInIntent for the Google’s sing-in process.


In the very last chapter, where we have implemented the authtentication for Facebook, we already defined this method.

We are going to extend it with the returned data from Google.


If the request code is equal to the code (RC_SIGN_IN) what we have provided at the beginning of the FirebaseViewModel::class, then we can ask for the signed in account’s detailes.

The handleSignInResult() method is still in red, because we are going to implement it now.


The last method in the FirebaseViewModel::class will get the result from the registration.


Here we will run again a suspend function from the UserRepositoryImpl::class, so we have to launch the viewModelScope, which is the scope of the ViewModel provided by Kotlin Coroutines.

Inside fo the CoroutineScope we will request the signed-in account from Google. If we could get it, so it is not null, then we will request the credential also, what we can use to call the signInWithCredential method, which is located in the UserRepositoryImpl::class.

Now we can use again the familiar when-expression to check the result.

      • Result.Success: After some .let call, which we can prevent the call of on null value, we gonna create a User object from the FirebaseUser. Thenafter the _user variable will be handed over to Firestore, and a document will be created inside of the “user” collection.
      • Result.Error: When we are going to get an exception from the queries, then the process will be stopped and we gonna notify the user about the exception.
      • Result.Canceled: If the process will be canceled, then we will notify the user about it also.

The whole method is inside of a try-catch, to be sure that our app won’t crash at runtime.

Step 3 – The RegisterActivity

The implementation of the methods in the FirebaseViewModel::class has been finished. Now we can add some code to the RegisterActivity::class also.

In the activity we have only one task. It is the implementation of the click listener for the Google’s Button, which is fairly simple.

The click listener

You can call the setOnClickListener{} lambda on the iv_register_google ImageView. In our case we will use it as a Button.

Inside of this click listener, we will call the signInWithGoogle method from the FirebaseViewModel::class.

Step 4 – The LoginActivity

Before we run the app, we are going to add the click listener for the LoginActivity::class also. In this case it looks like this:

The click listener

Run the app

Finally, we can run the app and we can test our new feature.

When you click on the Google’s button, then a window will popup. In this window you can select which of your account you want to use for the registration.

ApiException 10

Sometimes it can happen, that the app won’t work. When you click on the signup button of Google, then in the Logcat of Android Studio you will get back the exception of “ApiException 10”. It happens, because the generated default_web_client_id is wrong. You can get the correct one by clicking on the link below.

Google Developer Console

From the top menu you can select your project and wait until the page loading.

Scroll down to the OAuth 2.0 Client IDs section and copy the Client Id.

Now open the FirebaseViewModel::class. There, inside of the signInWithGoogle() method, we have provided the requestIdToken for the googleSignInOptions variable. Remove the string, and paste there the copied id.

Run again the app again, and it will work properly. 


The source code is available on GitHub

GitHub – google branch


I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉

Have a nice a day! 🙂



Follow and like us:
Click to rate this post!
[Total: 2 Average: 5]

2 thoughts on “Firebase authentication – Google”

Leave a comment