Updated:
This guide builds email and password sign-up, log-in and log-out with Firebase Authentication in Kotlin. It uses coroutines (await()) instead of callbacks, so the code reads top to bottom, and it handles the common errors. The examples use Jetpack Compose, but the repository and ViewModel work the same with XML views.
2026 update: the old firebase-auth-ktx library is no longer used. Google stopped releasing the KTX modules in July 2025 and removed them from the Firebase BoM 34.0.0; the Kotlin APIs now live in the main firebase-auth module (Firebase migration guide). The code below uses the current setup.
What you need
- An Android project already connected to Firebase (a
google-services.jsonin yourapp/folder). If not, follow Getting started with Firebase on Android first. minSdk23 or higher – the current minimum for Firebase.- In the Firebase console: Authentication → Sign-in method → Email/Password → Enable.
Step 1: Add the dependencies
In app/build.gradle.kts, use the Firebase BoM so you don’t have to pick versions for each Firebase library:
dependencies {
// Firebase BoM – check the Firebase release notes for the newest version
implementation(platform("com.google.firebase:firebase-bom:35.0.2"))
implementation("com.google.firebase:firebase-auth")
// Adds await() for Firebase Tasks
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.10.2")
// ViewModel for Compose
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.9.4")
}Moving from an older project? Replace firebase-auth-ktx with firebase-auth, and change imports such as com.google.firebase.auth.ktx.auth to com.google.firebase.auth.auth and com.google.firebase.ktx.Firebase to com.google.firebase.Firebase.
Step 2: Write an AuthRepository with suspend functions
await() suspends until Firebase finishes, and throws an exception if it fails. Wrapping each call in runCatching turns that into a Result the UI can show.
import com.google.firebase.Firebase
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.auth.auth
import kotlinx.coroutines.tasks.await
class AuthRepository(
private val auth: FirebaseAuth = Firebase.auth
) {
val currentUser: FirebaseUser? get() = auth.currentUser
suspend fun signUp(email: String, password: String): Result<FirebaseUser> = runCatching {
auth.createUserWithEmailAndPassword(email, password).await().user!!
}
suspend fun logIn(email: String, password: String): Result<FirebaseUser> = runCatching {
auth.signInWithEmailAndPassword(email, password).await().user!!
}
suspend fun sendPasswordReset(email: String): Result<Unit> = runCatching {
auth.sendPasswordResetEmail(email).await()
Unit
}
fun logOut() = auth.signOut()
}Step 3: Turn Firebase errors into readable messages
import com.google.firebase.FirebaseNetworkException
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException
import com.google.firebase.auth.FirebaseAuthUserCollisionException
import com.google.firebase.auth.FirebaseAuthWeakPasswordException
fun Throwable.toAuthMessage(): String = when (this) {
is FirebaseAuthWeakPasswordException -> "Password must be at least 6 characters."
is FirebaseAuthUserCollisionException -> "An account with this email already exists."
is FirebaseAuthInvalidCredentialsException -> "Email or password is incorrect."
is FirebaseNetworkException -> "No internet connection. Try again."
else -> message ?: "Something went wrong."
}New Firebase projects have email enumeration protection turned on, so a wrong email and a wrong password both come back as invalid credentials. That is intentional: it stops people checking which emails have accounts.
Step 4: Add a ViewModel
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
data class AuthUiState(
val loading: Boolean = false,
val error: String? = null,
val userEmail: String? = null
)
class AuthViewModel(
private val repo: AuthRepository = AuthRepository()
) : ViewModel() {
var state by mutableStateOf(AuthUiState(userEmail = repo.currentUser?.email))
private set
fun signUp(email: String, password: String) = run { repo.signUp(email.trim(), password) }
fun logIn(email: String, password: String) = run { repo.logIn(email.trim(), password) }
fun logOut() {
repo.logOut()
state = AuthUiState()
}
private fun run(block: suspend () -> Result<com.google.firebase.auth.FirebaseUser>) {
viewModelScope.launch {
state = state.copy(loading = true, error = null)
block()
.onSuccess { state = AuthUiState(userEmail = it.email) }
.onFailure { state = state.copy(loading = false, error = it.toAuthMessage()) }
}
}
}Step 5: Build the screen
@Composable
fun AuthScreen(vm: AuthViewModel = viewModel()) {
val state = vm.state
if (state.userEmail != null) {
Column(Modifier.padding(24.dp)) {
Text("Logged in as ${state.userEmail}")
Button(onClick = vm::logOut) { Text("Log out") }
}
return
}
var email by rememberSaveable { mutableStateOf("") }
var password by rememberSaveable { mutableStateOf("") }
Column(Modifier.padding(24.dp), verticalArrangement = Arrangement.spacedBy(12.dp)) {
OutlinedTextField(email, { email = it }, label = { Text("Email") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email))
OutlinedTextField(password, { password = it }, label = { Text("Password") },
visualTransformation = PasswordVisualTransformation(),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password))
state.error?.let { Text(it, color = MaterialTheme.colorScheme.error) }
Row(horizontalArrangement = Arrangement.spacedBy(12.dp)) {
Button(onClick = { vm.logIn(email, password) }, enabled = !state.loading) { Text("Log in") }
OutlinedButton(onClick = { vm.signUp(email, password) }, enabled = !state.loading) { Text("Sign up") }
}
if (state.loading) CircularProgressIndicator()
}
}Firebase keeps the user signed in between app launches, so currentUser is already set when the app reopens. That is why the ViewModel reads it at start-up.
Optional: watch sign-in changes as a Flow
If several screens need to react when the user logs in or out, expose the auth state as a Flow:
fun FirebaseAuth.userFlow(): Flow<FirebaseUser?> = callbackFlow {
val listener = FirebaseAuth.AuthStateListener { trySend(it.currentUser) }
addAuthStateListener(listener)
awaitClose { removeAuthStateListener(listener) }
}Optional: verify the email address
suspend fun sendVerification(): Result<Unit> = runCatching {
Firebase.auth.currentUser?.sendEmailVerification()?.await()
Unit
}
// Later: Firebase.auth.currentUser?.reload()?.await(); check isEmailVerifiedCommon problems
| Problem | Fix |
|---|---|
CONFIGURATION_NOT_FOUND or “sign-in method disabled” | Enable Email/Password under Authentication → Sign-in method. |
| App crashes with “Default FirebaseApp is not initialized” | google-services.json is missing from app/, or the Google services plugin isn’t applied. |
Unresolved reference ktx | Remove -ktx dependencies and update imports (Step 1). |
await not found | Add kotlinx-coroutines-play-services and import kotlinx.coroutines.tasks.await. |
Video tutorial (older XML version)
FAQ
Do I still need firebase-auth-ktx?
No. Use firebase-auth with the Firebase BoM; the Kotlin extensions are part of it now.
Is the user still logged in after closing the app?
Yes. Firebase stores the session on the device until you call signOut() or the user is deleted or disabled.
Can I save the user’s name and phone number?
Firebase Auth only stores email, display name and photo. For other fields, save a document in Firestore – see custom user fields with Firebase.
More Firebase guides
- Getting started: add Firebase to your Android project
- Save custom user fields (name, phone) with Firebase Auth and Firestore
- All Firebase tutorials
Keep reading



