Skip to main content

Overview

The AuthService class provides authentication functionality using Firebase Authentication. It handles user login, registration, password recovery, and authentication state management. Location: com.demodogo.ev_sum_2.services.AuthService

Constructor

class AuthService(
    private val repo: AuthRepository = AuthRepository()
)
repo
AuthRepository
default:"AuthRepository()"
The authentication repository instance. Defaults to a new AuthRepository instance.

Methods

login

Authenticates a user with email and password.
suspend fun login(email: String, password: String)
email
String
required
The user’s email address
password
String
required
The user’s password
Throws: Exception if authentication fails with mapped Firebase error message

Usage Example

val authService = AuthService()

try {
    authService.login("[email protected]", "password123")
    // User successfully logged in
} catch (e: Exception) {
    // Handle authentication error
    println("Login failed: ${e.message}")
}

register

Registers a new user with email and password.
suspend fun register(email: String, password: String)
email
String
required
The new user’s email address
password
String
required
The new user’s password
Throws: Exception if registration fails with mapped Firebase error message

Usage Example

val authService = AuthService()

try {
    authService.register("[email protected]", "securePassword123")
    // User successfully registered
} catch (e: Exception) {
    // Handle registration error
    println("Registration failed: ${e.message}")
}

recover

Sends a password reset email to the specified email address.
suspend fun recover(email: String)
email
String
required
The email address to send the password reset link to
Throws: Exception if the password reset request fails

Usage Example

val authService = AuthService()

try {
    authService.recover("[email protected]")
    // Password reset email sent
} catch (e: Exception) {
    println("Password reset failed: ${e.message}")
}

logout

Logs out the current user.
fun logout()

Usage Example

val authService = AuthService()
authService.logout()

isLoggedIn

Checks if a user is currently logged in.
fun isLoggedIn(): Boolean
Returns: Boolean - true if a user is logged in, false otherwise

Usage Example

val authService = AuthService()
if (authService.isLoggedIn()) {
    // User is logged in
} else {
    // User is not logged in
}

authStateFlow

Provides a Flow that emits authentication state changes.
fun authStateFlow(): Flow<FirebaseUser?>
Returns: Flow<FirebaseUser?> - A Flow that emits the current FirebaseUser or null when the user is not authenticated

Usage Example

val authService = AuthService()

lifecycleScope.launch {
    authService.authStateFlow().collect { user ->
        if (user != null) {
            println("User logged in: ${user.email}")
        } else {
            println("User logged out")
        }
    }
}

currentEmail

Retrieves the email address of the currently logged-in user.
fun currentEmail(): String?
Returns: String? - The current user’s email address, or null if not logged in

Usage Example

val authService = AuthService()
val email = authService.currentEmail()
if (email != null) {
    println("Current user: $email")
} else {
    println("No user logged in")
}

Error Handling

All Firebase authentication errors are mapped through the mapFirebaseAuthError private method, which extracts the error message or provides a default “Error de autenticación” message.
Always wrap authentication operations in try-catch blocks to handle potential exceptions:
try {
    authService.login(email, password)
} catch (e: Exception) {
    // Display error to user
    showError(e.message ?: "Authentication failed")
}

See Also

Build docs developers (and LLMs) love