Simple Steps To Sign-In With Google On Firestore Android

Are you want to set up a user Sign-in with google in your android application, In this tutorial I have explained about login with firebase Sign-In in android.

Firebase Authentication is a secure authentication system provided by Google that allows developers to manage user authentication for their applications. It enables developers to create custom authentication flows and integrate with popular identity providers such as Facebook, Twitter, and Google.

Steps to create and configure Android App on Google Firebase Account

Create a Firebase developer account at Firebase Console.

  1. Go to the Firebase console.
  2. Click Add Project > Enter Project Name > Continue > Create Project
Create a new project on the Firebase console

3.Once the project is created, Select the ‘Android’ platform SDK to configure the android application.

new android project

4.Enter the application package name and register the app.

5.Once registered, you can able to download the google-service.json file to integrate it into the Android application.

google-service.json file

6.Then click Next > and Continue to console.

7.also enable the google sign-in on firebase authentication.

go to Firebase Console > Authentication > Sign-in method

enable google sign

Now we are done with the project set up on the firebase console. let’s move to the android studio to create an android project.

Android Studio Firebase Login Set up

Create a new project, go to tools > Firebase.

android firebase tools

It will open the Assistant for firebase. Select Authentication > Authentication With Google [kotlin].

firebase authentication assistant

Then, click Connect to firebase. It will redirect to the firebase console and ask you to select the project for the login setup. choose the newly created project and switch back to android studio.

It will sync all the project configurations to the android studio project like the google-service.json file will be copied to your app directory.

Once the firebase is connected to the app. then click on the Add the firebase authentication SDK to your app. It will add the dependencies for the firebase auth configurations. The dependencies are,

In the project-level build.gradle, add the classpath,

buildscript {
        dependencies {
            classpath ''

In the module-level build.gradle file.

plugins {

        id ''
    dependencies {
        implementation ''
        implementation platform('')
        implementation ''
        implementation ''

done with adding project dependencies. let’s create a login screen UI design.


<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android=""

            android:text="Login Sample App"
            app:layout_constraintVertical_bias="0.31" />

            android:text="Login With Goole"
            app:layout_constraintTop_toBottomOf="@+id/label" />


After done with the login screen design, your Login Activity needs to initialize the sign-in request. To create a sign-in request we need a web client ID for our project. Go to the credential console and copy the web client ID.

firebase console credential page

also, other important properties are,

setFilterByAuthorizedAccounts – to true so only the Google accounts that the user has authorized before will show up in the credential list. This can help prevent a new account from being created when the user has an existing account registered with the application.

setAutoSelectEnabled – Automatic sign-in is possible when the following criteria are met:

  • The user has exactly one credential saved for your app. That is, one saved password or one saved Google Account.
  • The user hasn’t disabled automatic sign-in in their Google Account settings.
lateinit var oneTapClient: SignInClient
  lateinit var signInRequest: BeginSignInRequest

  oneTapClient = Identity.getSignInClient(this)
  signInRequest = BeginSignInRequest.builder()
  // Only show accounts previously used to sign in.
  // Automatically sign in when exactly one credential is retrieved.

After the sign-in request is ready, on click of the login button need to call beginSignIn() with the sign-in request we created. This will start the pending intent to handle the login result.

 btnLoginWithGoogle.setOnClickListener {
                oneTapClient.beginSignIn(signInRequest).addOnSuccessListener { result ->
                    try {

                            result.pendingIntent.intentSender, REQ_ONE_TAP,
                            null, 0, 0, 0, null
                    } catch (e: IntentSender.SendIntentException) {
                        Log.e("Error", "Couldn't start One Tap UI: ${e.localizedMessage}")
                }.addOnFailureListener {
                    Log.d("Error", it.localizedMessage)

On click of the login button, it will launch the google authentication UI to enter username and password. If the user already logged in with the same account already it will just show the account to choose from.

Firebase sign in screen

After the user enters the username and password or selected the login account, it will redirect to the activity result. use the below code to get the authToken and logged-in user details.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)

            when (requestCode) {
                REQ_ONE_TAP -> {
                    try {
                        val credential = oneTapClient.getSignInCredentialFromIntent(data)
                        val idToken = credential.googleIdToken
                        val username =
                        val password = credential.password
                        when {
                            idToken != null -> {
                                // Got an ID token from Google. Use it to authenticate
                                // with your backend.
                                Log.d("Debug", "Got ID token.")
                            password != null -> {
                                // Got a saved username and password. Use them to authenticate
                                // with your backend.
                                mAuth.signInWithEmailAndPassword(username, password)
                                    .addOnSuccessListener {
                                        Toast.makeText(this, it.user?.email, Toast.LENGTH_SHORT)
                                    }.addOnFailureListener {
                                        Toast.makeText(this, it.message, Toast.LENGTH_SHORT)

                                Log.d("Debug", "Got password.")

                            else -> {
                                // Shouldn't happen.
                                Log.d("Error", "No ID token or password!")
                    } catch (e: ApiException) {
                        // ...

Check Logged-in User

To check the logged-in user, use the below code at the start of the application,

override fun onStart() {
        // Check if user is signed in (non-null) and update UI accordingly.
        var currentUser = auth.getCurrentUser()

Handle sign-out

When a user signs out of your app, call the One Tap client’s **signOut()** method. Calling signOut() disables automatic sign-in until the user signs in again.

btnSignOut.setOnClickListener {


Thanks for reading. Also, you can download the example from GitHub.


Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Posts