In this post I am going to display the complete flow of Google SignIn method using the Firebase Auth UI. Which means, we do not need to implement our own ui rather we can use the Firebase Auth UI to implement the Sign In process. We can Also Implement the other auth providers in similar way, like Facebook Auth, or Twitter Auth, or Email Auth Provider, But in today’s post we are only going to stick with Google auth provider using Firebase Auth Ui.
Login Activity
First of all we need to implement a simple Login Activity. This is just one button activity which will be responsible for checking if user is already authenticated then redirect user to MainActivity, otherwise present a button. Whenever user will press that button it will launch the Firebase Auth UI and rest will be handled by the Firebase Auth UI. Here is the simple XML view for the activity.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginSplash">
<Button
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign In with Google"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Code language: HTML, XML (xml)
Gradle File
Here are the changes which you need to make in the gradle file to import proper firebase libraries.
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("com.google.gms.google-services")
}
android {
namespace = "com.irebase.myapplication"
compileSdk = 35
defaultConfig {
applicationId = "com.irebase.myapplication"
minSdk = 28
targetSdk = 35
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation("androidx.core:core-ktx:1.16.0")
implementation("androidx.appcompat:appcompat:1.7.0")
implementation("com.google.android.material:material:1.12.0")
implementation("androidx.constraintlayout:constraintlayout:2.2.1")
implementation("androidx.activity:activity:1.8.0")
implementation("com.firebaseui:firebase-ui-auth:9.0.0")
// Import the BoM for the Firebase platform
// Make sure to use the latest version available from Firebase documentation
implementation(platform("com.google.firebase:firebase-bom:33.13.0")) // Use current latest version
// Declare the dependencies for the desired Firebase products without specifying versions
// For example, declare the dependencies for Firebase Authentication and Cloud Firestore
implementation("com.google.firebase:firebase-auth-ktx")
implementation("com.google.firebase:firebase-firestore")
implementation("androidx.credentials:credentials:1.5.0")
implementation("androidx.credentials:credentials-play-services-auth:1.5.0")
implementation("com.google.android.libraries.identity.googleid:googleid:1.1.1")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.2.1")
androidTestImplementation("androidx.test.espresso:espresso-core:3.6.1")
}
Code language: JavaScript (javascript)
Comple Login Flow
Here is the complete code which is used to implement Firebase Authentication using Firebase Auth UI.
package com.irebase.myapplication
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResultLauncher
import androidx.appcompat.app.AppCompatActivity
import com.firebase.ui.auth.AuthUI
import com.firebase.ui.auth.FirebaseAuthUIActivityResultContract
import com.google.firebase.auth.FirebaseAuth
import com.firebase.ui.auth.data.model.FirebaseAuthUIAuthenticationResult
import kotlin.collections.arrayListOf
class LoginSplash : AppCompatActivity() {
private lateinit var auth: FirebaseAuth
// Choose authentication providers
val providers = arrayListOf(
AuthUI.IdpConfig.EmailBuilder().build(),
// AuthUI.IdpConfig.PhoneBuilder().build(),
AuthUI.IdpConfig.GoogleBuilder().build(),
// AuthUI.IdpConfig.FacebookBuilder().build(),
// AuthUI.IdpConfig.TwitterBuilder().build(),
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_login_splash)
auth = FirebaseAuth.getInstance()
// See: https://developer.android.com/training/basics/intents/result
val signInLauncher = registerForActivityResult(
FirebaseAuthUIActivityResultContract(),
) { res ->
this.onSignInResult(res)
}
val signInButton: Button = findViewById(R.id.sign_in_button) // Assuming you have this button
signInButton.setOnClickListener {
// Create and launch sign-in intent
val signInIntent = AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build()
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
Log.i("SignInActivity", "Sign-in successful! User: ${user?.displayName}")
// Navigate to your main activity or update UI
startMainActivity()
finish() // Close the sign-in activity
} 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.
if (response == null) {
Log.w("SignInActivity", "Sign-in cancelled by user")
Toast.makeText(this, "Sign-in cancelled", Toast.LENGTH_SHORT).show()
} else {
Log.e("SignInActivity", "Sign-in error: ${response.error?.localizedMessage}", response.error)
Toast.makeText(this, "Sign-in failed: ${response.error?.localizedMessage}", Toast.LENGTH_LONG).show()
}
}
}
override fun onStart() {
super.onStart()
// Check if user is signed in (non-null) and update UI accordingly.
val currentUser = FirebaseAuth.getInstance().currentUser
if (currentUser != null) {
Log.i("SignInActivity", "User already signed in: ${currentUser.displayName}")
// User is already logged in, maybe navigate directly to main activity
startMainActivity()
finish() // Finish sign-in activity if already logged in
} else {
// No user is signed in, stay on sign-in screen (or launch flow automatically if desired)
Log.i("SignInActivity", "No user signed in.")
// Optionally, you could call launchSignInFlow() here to automatically start it
Toast.makeText(baseContext,"No user ",Toast.LENGTH_SHORT)
}
}
private fun startMainActivity(){
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
}
Code language: Kotlin (kotlin)