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
First Time Users: Call registerWithEmailAndPassword or registerWithGoogle
Returning Users: Call signInWithSavedCredentials or specific sign-in methods
Session Check: Use getCurrentUser to check authentication state
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/passwordFirebaseAuthUserCollisionException- Email already in useFirebaseAuthInvalidUserException- User account doesn't existFirebaseNetworkException- Network connectivity issuesCancellationException- 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
Functions
Gets the currently authenticated user, if any.
Registers a new user with their Google account using Google Sign-In.
Signs in a user with email and password using Firebase Authentication.
Signs in an existing user with their Google account using Google Sign-In.
Attempts to sign in using credentials saved in Android's Credential Manager.