Skip to content

Commit

Permalink
Add possibility to provide custom firebase auth instance
Browse files Browse the repository at this point in the history
  • Loading branch information
warting committed Oct 1, 2021
1 parent 4c2ebd4 commit 9bd1764
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
1 change: 1 addition & 0 deletions auth/api/auth.api
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public final class se/warting/firebasecompose/FirebaseComposeAuthKt {

public final class se/warting/firebasecompose/ProvideAndroidCompositionLocalsKt {
public static final fun ProvideFirebaseComposeAuthLocals (Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;I)V
public static final fun getLocalFirebaseAuth ()Landroidx/compose/runtime/ProvidableCompositionLocal;
public static final fun getLocalFirebaseAuthState ()Landroidx/compose/runtime/ProvidableCompositionLocal;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import kotlinx.coroutines.tasks.await

@Composable
internal fun rememberMutableFirebaseAuthState(): FirebaseAuthState {

val firebaseAuth = LocalFirebaseAuth.current

val firebaseAuthState = remember {
MutableFirebaseAuthState()
MutableFirebaseAuthState(firebaseAuth)
}

val authStateListener = remember {
Expand All @@ -58,40 +58,40 @@ internal fun rememberMutableFirebaseAuthState(): FirebaseAuthState {
return firebaseAuthState
}

internal class MutableFirebaseAuthState : FirebaseAuthState {

private val f = Firebase.auth
internal class MutableFirebaseAuthState(
private val firebaseAuth: FirebaseAuth
) : FirebaseAuthState {

private var _isLoggedIn by mutableStateOf(f.currentUser != null)
private var _isLoggedIn by mutableStateOf(firebaseAuth.currentUser != null)

override val isLoggedIn: Boolean
get() = _isLoggedIn

override fun logout() {
f.signOut()
firebaseAuth.signOut()
}

override fun signInAnonymously() {
f.signInAnonymously()
firebaseAuth.signInAnonymously()
}

override fun addAuthStateListener(listener: FirebaseAuth.AuthStateListener) {
f.addAuthStateListener(listener)
firebaseAuth.addAuthStateListener(listener)
}

override fun removeAuthStateListener(listener: FirebaseAuth.AuthStateListener) {
f.removeAuthStateListener(listener)
firebaseAuth.removeAuthStateListener(listener)
}

override fun updateLoggedInState() {
_isLoggedIn = f.currentUser != null
_isLoggedIn = firebaseAuth.currentUser != null
}

override suspend fun getItToken(forceRefresh: Boolean): String? {
return f.currentUser?.getIdToken(forceRefresh)?.await()?.token
return firebaseAuth.currentUser?.getIdToken(forceRefresh)?.await()?.token
}

override fun getUserId(): String? {
return f.currentUser?.uid
return firebaseAuth.currentUser?.uid
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ package se.warting.firebasecompose
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.staticCompositionLocalOf
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase

val LocalFirebaseAuthState = staticCompositionLocalOf<FirebaseAuthState> {
noLocalProvidedFor("FirebaseAuth")
}

val LocalFirebaseAuth = staticCompositionLocalOf {
Firebase.auth // Default instance of auth
}

@Composable
fun ProvideFirebaseComposeAuthLocals(
content: @Composable () -> Unit
Expand Down

0 comments on commit 9bd1764

Please sign in to comment.