Kotlin Firebase Authentication – Google Sign In | Android
Tags: kotlin, firebase, android, google
Published: 2021-04-24 14:24:22

https://grokonez.com/android/kotlin-firebase-authentication-google-sign-in-android

Kotlin Firebase Authentication – Google Sign In | Android

In previous <a href="https://grokonez.com/android/kotlin-firebase-authentication-how-to-sign-up-sign-in-sign-out-verify-email-android">post</a>, 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:
- <a href="https://grokonez.com/android/kotlin-firebase-authentication-how-to-sign-up-sign-in-sign-out-verify-email-android">Kotlin Firebase Authentication – How to Sign Up, Sign In, Sign Out, Verify Email | Android</a>
- <a href="https://grokonez.com/android/kotlin-firebase-authentication-send-reset-password-email-forgot-password-android">Kotlin Firebase Authentication – Send Reset Password Email / Forgot Password | Android</a>

<!--more-->
<h2>I. Way to do</h2>
<h3>1. Add Firebase to Android App</h3>
- Read <a href="https://grokonez.com/android/kotlin-firebase-authentication-how-to-sign-up-sign-in-sign-out-verify-email-android">Kotlin Firebase Authentication – How to Sign Up, Sign In, Sign Out, Verify Email | Android</a> and follow steps to enable and implement Firebase Auth in your Android App.

- Add dependency: <strong>build.gradle</strong> (App-level):
<pre><code class="language-java">
dependencies {
    // ...
    implementation 'com.google.android.gms:play-services-auth:11.0.4'
}
</code></pre>
<h3>2. Enable Firebase Auth for Google Account</h3>
Go to Your <a href="https://console.firebase.google.com">Firebase Project Console</a> -> <strong>Authentication</strong> -> <strong>SIGN-IN METHOD</strong> -> Enable <strong>Google</strong>:
<img src="https://grokonez.com/wp-content/uploads/2017/08/firebase-google-signin-enable-console.png" alt="firebase-google-signin-enable-console" width="699" height="393" class="alignnone size-full wp-image-7181" />
<h3>3. Steps to authenticate with Firebase</h3>
<h4>3.1 Integrate Google Sign In into App</h4>
1- Add the Google Sign-In button:
<pre><code class="language-html">
<com.google.android.gms.common.SignInButton
            android:id="@+id/btn_sign_in"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
</code></pre>
2- Configure Google Sign In object to request the user data:
When you configure the <code>GoogleSignInOptions</code> object, call <code>requestIdToken</code>:
<pre><code class="language-java">
override fun onCreate(savedInstanceState: Bundle?) {
    // ...

    val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(WEB_CLIENT_ID)
            .requestEmail()
            .build()
}</code></pre>
With <code>WEB_CLIENT_ID</code> from <a href="https://console.developers.google.com/apis/credentials" rel="nofollow">Credentials page</a> in the API Console -> OAuth 2.0 client IDs (Type <strong>Web Application</strong>).

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

More at:

https://grokonez.com/android/kotlin-firebase-authentication-google-sign-in-android

Kotlin Firebase Authentication – Google Sign In | Android