Kotlin Coroutine Extensions for Firebase 🔥
This is still a work in progress, and relies on Kotlin's experimental
Flow
objects. API's may change as needed. Proceed with caution.
You can observe the current auth state by collecting updates on the AuthState Flow:
firebaseAuth.authStateFlow.collect { authState ->
Log.d("Example", "User changed to: ${authState.currentUser?.uid}")
}
For convenience, the current user can be observed directly with
the userFlow
extension:
firebaseAuth.currentUser.collect { currentUser ->
Log.d("Example", "User changed to: ${currentUser?.uid}")
}
Firebase's ID Token can be observed with the idTokenFlow
extension:
firebaseAuth.idTokenFlow.collect { tokenResult ->
Log.d("Example", "ID Token changed to: ${tokenResult?.token}")
}
Firestore collections can be observed with the snapshotFlow
extension:
firebaseFirestore
.collection("users")
.snapshotFlow
.collect { snapshot ->
Log.d("Example", "New data for collection documents. Size: ${snapshot?.documents?.size}")
}
Firestore documents can be observed with the snapshotFlow
extension:
firebaseFirestore
.collection("users")
.document("id-123")
.snapshotFlow
.collect { snapshot ->
Log.d("Example", "New data for user, ID: ${snapshot?.id}")
}
Any Task<T>
object can be suspended for a result using .await()
:
val authResult = firebaseAuth.createUserWithEmailAndPassword("johndoe@example.com", "notsecure").await()