Skip to content

Instantly share code, notes, and snippets.

@molidev8
Created September 17, 2022 07:42
Show Gist options
  • Save molidev8/95b8e65327a1b3350944fe3da0007a18 to your computer and use it in GitHub Desktop.
Save molidev8/95b8e65327a1b3350944fe3da0007a18 to your computer and use it in GitHub Desktop.
Firebase Authentication
private val auth: FirebaseAuth = FirebaseAuth.getInstance()
// AuthState can be read to know if the user is logged in
val authState = object : LiveData<FirebaseUser>() {
private val authStateListener = FirebaseAuth.AuthStateListener { firebaseAuth ->
value = firebaseAuth.currentUser
}
override fun onActive() {
auth.addAuthStateListener(authStateListener)
}
override fun onInactive() {
auth.removeAuthStateListener(authStateListener)
}
}
fun getUserID() = auth.uid!!
fun signOut() {
auth.signOut()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment