Authenticating with Google Sign-In and Firebase Auth and Firebase UI on Kotlin for Android

Authenticating with Google Sign-In and Firebase Auth and Firebase UI on Kotlin for Android

·

3 min read

This is the first installment of my weekly series on "New Android Development." I want to get started on Firestore immediately, but before you can do anything interesting (e.g., multi-user) you need to have authentication. Otherwise, there is no way to distinguish users and any permission that you grant to any user is granted to all users (thus the image above of a monster/hacker trying to get to your goods/data - see).

So what is the fastest way to have some form of authentication: FirebaseUI.

FirebaseUI is a library built on top of the Firebase Authentication SDK that provides drop-in UI flows for use in your app.

There are dozens of videos, blog posts, and other resources to choose from. But these are the most concise set of steps that I was able to use to get up-and-running with Google Sign-In and Firebase Auth and Firebase UI on Kotlin for Android (with help from a friend - thanks AC!).

Here are the major steps for Getting Google Sign-in using Firebase Authentication Up And Running:

  1. Create a Project in your Firebase Console

    1. Click on the "Authentication" tile and click "Google" under Providers.

    2. Click enable and select one of your "project supported emails" that will inform users who you are/who they are Authenticating with

    3. Click save.

    4. Go to Project Settings (click the gear next to Project Overview)

    5. Scroll down on the "general" tab for your project and click the "Add App" button

    6. Click the Android Icon

  2. Shift to Android Studio

    1. Create a New Project > Empty Activity (remember your package name, or get it from app > build.gradle > applicationId)

    2. Click on the Terminal tab for this project and type the following to get your SHA1 fingerprint

       ./gradlew signingReport
      
    3. Copy the SHA1

  3. Head back to your Firebase Console on the Add App panel from above

    1. Enter your Android Package Name

    2. Paste your SHA1 in the "Debug signing certificate SHA-1 (optional)" - it is not optional for you, because you are using Google Sign-In.

    3. Click "Register app"

    4. Download your "google-services.json" file. This essentially connects your Android application to this Firestore app.

Head back to Android Studio

  1. Drag the downloaded "google-services.json" file into your app directory

  2. Accept the option to Refactor.

  3. You should see your "google-services.json" file next to your build.gradle file in your app directory.

  4. On the top navigation, click Tools > Firebase > Authentication > > Authenticate Using Google [KOTLIN]

  5. You will see some options - you have manually done #1 & #3 in the steps above, so click #2 to "Add the Firebase Authentication SDK to your app"

  6. Steps 4-8 in Android Studio are best to ignore, and focus on this tutorial https://firebase.google.com/docs/auth/android/firebaseui#sign_in (starting at the Sign In section)

  7. Add a dependency to your app > build.gradle file for FirebaseUI (and click Sync Now)

implementation 'com.firebaseui:firebase-ui-auth:7.2.0'
  1. Add the following imports to your MainActivity.kt file
import com.firebase.ui.auth.AuthUI
import com.firebase.ui.auth.FirebaseAuthUIActivityResultContract
import com.firebase.ui.auth.data.model.FirebaseAuthUIAuthenticationResult
import com.google.firebase.auth.FirebaseAuth
  1. Replace your OnCreate method with the following
private val signInLauncher = registerForActivityResult(
        FirebaseAuthUIActivityResultContract()
    ) { res ->
        this.onSignInResult(res)
    }

    val providers = arrayListOf(
        AuthUI.IdpConfig.EmailBuilder().build(),
        AuthUI.IdpConfig.GoogleBuilder().build())

    // Create and launch sign-in intent
    val signInIntent = AuthUI.getInstance()
        .createSignInIntentBuilder()
        .setAvailableProviders(providers)
        .build()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        signInLauncher.launch(signInIntent)
    }

    private fun onSignInResult(result: FirebaseAuthUIAuthenticationResult) {
        val response = result.idpResponse
        if (result.resultCode == RESULT_OK) {
            // Successfully signed in
            val user = FirebaseAuth.getInstance().currentUser
            // ...
        } else {
            // Sign in failed. If response is null the user canceled the
            // sign-in flow using the back button. Otherwise check
            // response.getError().getErrorCode() and handle the error.
            // ...
        }
    }

Conclusion

Click build and there you have it. You can sign in with Google, and this information is automatically available for you to use in Firestore. Which is where we are off to next.