Kotlin 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 Kotin Android App.

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

I. Way to do

1. Add Firebase to Android App

– Read Kotlin Firebase Authentication – How to Sign Up, Sign In, Sign Out, Verify Email | Android and follow steps to enable and implement Firebase Auth in your Android App.

– Add dependency: build.gradle (App-level):

2. Enable Firebase Auth for Google Account

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

3. Steps to authenticate with Firebase
3.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:

3.2 signIn() method

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

3.3 Initialize the FirebaseAuth instance

In the Activity onCreate() method:

3.4 Check if the user is currently signed in

In Activity onStart() method:

3.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:

4. Sign Out User and Disconnect Account
4.1 Sign out User

4.2 Disconnect Account

II. Practice

1. Goal

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

kotlin-firebase-google-signin-app-demo

2. Technology

– Gradle 3.0.1
– Android Studio 3.x
– Firebase Android SDK 11.x

3. Project Structure

kotlin-firebase-google-signin-structure

4. Step by step
4.1 Create Android Project & add Firebase Auth

– Generate new Android Project with package com.javasampleapproach.kotlin.authgoogle.
– Follow these steps to add Firebase to Android Project.
– Don’t forget to enable Firebase Auth for Google Account:
firebase-google-signin-enable-console

– Add dependency: build.gradle (App-level):

4.2 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.3 Activity

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

4.4 Run & Check result

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

firebase-google-signin-app-demo

– Firebase Console:
kotlin-firebase-google-signin-app-console

III. Source code

Koltin-Auth-Google-SignIn

By JavaSampleApproach | December 16, 2017.


Related Posts


Got Something To Say:

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

*