AuthDataSource

interface AuthDataSource

Interface defining data source operations for Firebase Authentication.

This interface provides a unified abstraction over Firebase Authentication and Android's Credential Manager API. It supports multiple authentication methods including email/password and Google Sign-In, with built-in credential saving for seamless re-authentication.

Authentication Flow

  1. First Time Users: Call registerWithEmailAndPassword or registerWithGoogle

  2. Returning Users: Call signInWithSavedCredentials or specific sign-in methods

  3. Session Check: Use getCurrentUser to check authentication state

  4. Sign Out: Call signOut to clear authentication state

Thread Safety

All suspend functions should be called from a coroutine context with appropriate dispatcher. The implementation ensures thread safety by using Firebase's built-in thread handling.

Error Handling

All authentication methods can throw exceptions:

  • FirebaseAuthInvalidCredentialsException - Wrong email/password

  • FirebaseAuthUserCollisionException - Email already in use

  • FirebaseAuthInvalidUserException - User account doesn't exist

  • FirebaseNetworkException - Network connectivity issues

  • CancellationException - User cancelled the authentication flow

Wrap calls in suspendRunCatching for proper error handling.

Example Usage

class AuthRepository @Inject constructor(
private val authDataSource: AuthDataSource
) {
// Check current auth state
fun isSignedIn(): Boolean = authDataSource.getCurrentUser() != null

// Sign in with saved credentials (auto-login)
suspend fun autoSignIn(activity: Activity): Result<AuthUser> =
suspendRunCatching {
authDataSource.signInWithSavedCredentials(activity)
}

// Email/password sign in
suspend fun signIn(email: String, password: String): Result<AuthUser> =
suspendRunCatching {
authDataSource.signInWithEmailAndPassword(email, password)
}

// Google Sign-In
suspend fun signInWithGoogle(activity: Activity): Result<AuthUser> =
suspendRunCatching {
authDataSource.signInWithGoogle(activity)
}

// Sign out
suspend fun signOut(): Result<Unit> =
suspendRunCatching {
authDataSource.signOut()
}
}

See also

FirebaseAuth

Functions

Link copied to clipboard
abstract fun getCurrentUser(): AuthUser?

Gets the currently authenticated user, if any.

Link copied to clipboard
abstract suspend fun registerWithEmailAndPassword(name: String, email: String, password: String, activity: Activity): AuthUser

Registers a new user with email and password, then saves credentials to Credential Manager.

Link copied to clipboard
abstract suspend fun registerWithGoogle(activity: Activity): AuthUser

Registers a new user with their Google account using Google Sign-In.

Link copied to clipboard
abstract suspend fun signInWithEmailAndPassword(email: String, password: String): AuthUser

Signs in a user with email and password using Firebase Authentication.

Link copied to clipboard
abstract suspend fun signInWithGoogle(activity: Activity): AuthUser

Signs in an existing user with their Google account using Google Sign-In.

Link copied to clipboard
abstract suspend fun signInWithSavedCredentials(activity: Activity): AuthUser

Attempts to sign in using credentials saved in Android's Credential Manager.

Link copied to clipboard
abstract suspend fun signOut()

Signs out the currently authenticated user from Firebase and clears local session.