Firebase Authentication – Google Sign In | Android

In previous post, we had built a simple register/login Android App using Firebase Email & Password authentication. Now we can let users authenticate with Firebase using Google Accounts by integrating Google Sign In into Android App.

Related articles:
Firebase Authentication – How to Sign Up, Sign In, Sign Out, Verify Email | Android
Firebase Authentication – Send Reset Password Email / Forgot Password | Android

I. Way to do

0. Add Firebase to Android App
0.1 Create Firebase Project and Add Firebase Config file

Follow this guide to create Firebase Project and generate google-services.json file and move it into your Android App root directory.

*Note: You need SHA-1 Key in this example.

0.2 Add dependencies

build.gradle (project-level):

build.gradle (App-level):

1. Enabling Firebase Auth for Google Account

Go to Your Firebase Project Console -> Authentication -> SIGN-IN METHOD -> Enable Google:
firebase-google-signin-enable-console

2. Steps to authenticate with Firebase
2.1 Integrate Google Sign In into App

1- Add the Google Sign-In button:

2- Configure Google Sign In object to request the user data:
When you configure the GoogleSignInOptions object, call requestIdToken:

With WEB_CLIENT_ID from Credentials page in the API Console -> OAuth 2.0 client IDs (Type Web Application).

3- Configure GoogleApiClient object with access to the Google Sign In API and the options above:

2.2 signIn() method

In the activity’s onActivityResult() method, we retrieve the sign-in result with Auth.GoogleSignInApi.getSignInResultFromIntent():

2.3 Initialize the FirebaseAuth instance

In the Activity onCreate() method:

2.4 Check if the user is currently signed in

In Activity onStart() method:

2.5 Authenticate with Firebase using the Firebase credential

For firebaseAuthWithGoogle() method above:
After a user successfully signs in (in onActivityResult), get an ID token from the GoogleSignInAccount object, exchange it for a Firebase credential, then authenticate with Firebase using the Firebase credential:

3. Sign Out User and Disconnect Account
3.1 Sign out User

3.2 Disconnect Account

4. Internet permission

Because we need internet while authenticating, so we should add the Internet permission in AndroidManifest.xml file:

II. Practice

1. Goal

We will build an Android App that can sign in/sign out, disconnect Google Account:

firebase-google-signin-app-demo

2. Technology

– Gradle 2.3.3
– Android Studio 2.x
– Firebase Android SDK 11.x

3. Project Structure

firebase-google-signin-structure

4. Step by step
4.1 Create Android Project

– Generate new Android Project with package com.javasampleapproach.firebasegooglesignin.
build.gradle (project-level):

build.gradle (App-level):

4.2 Create Firebase Project & Add Firebase Config file

– Follow this guide to generate google-services.json file and move it into your Android App root directory.

– Make sure that package_name in google-services.json has a correct value according to:
+ applicationId in build.gradle (App-level).
+ package in AndroidManifest.xml.
In this case, it is com.javasampleapproach.firebasegooglesignin.

firebase-google-signin-add-app

4.3 Enable Firebase Auth for Google Account

Go to Your Firebase Project Console -> Authentication -> SIGN-IN METHOD -> Enable Google:
firebase-google-signin-enable-console

4.4 Get server’s client ID

To find the OAuth 2.0 client ID:
– Open the Credentials page in the API Console.
– Select your Firebase Project.
– Scroll down to OAuth 2.0 client IDs, copy Client ID (Web Application Type).

4.5 Activity

WEB_CLIENT_ID is the Client ID we get in the previous step.

4.6 Add the Internet Permission

Open AndroidManifest.xml and add tag uses-permission:

4.7 Run & Check result

– Use Android Studio, build and Run your Android App.

firebase-google-signin-app-demo

– Firebase Console:
firebase-google-signin-add-user

III. Source code

FirebaseGoogleSignin


Related Posts


Got Something To Say:

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

*