-
Problem Description:I’m using Supabase Authentication with Kotlin Multiplatform and Native UIs, and I’m facing an issue where the I have set up a Expected Behavior:
Actual Behavior:
I believe the session status flow might not be refreshing correctly after the session changes. Code Overview:Here is the relevant code I’ve implemented in my app. 1. SupabaseAuthServiceThe class SupabaseAuthService {
private val supabase = createSupabaseClient(
supabaseUrl = "http://localhost:8000",
supabaseKey = "your-supabase-key"
) {
install(Auth) {
host = "your-app-id"
scheme = "your-app-scheme"
}
}
// Returns the current session status as a StateFlow
fun sessionStatus(): StateFlow<SessionStatus> {
return supabase.auth.sessionStatus
}
// Signs out the current user
suspend fun signOut() {
println("signOut")
supabase.auth.signOut()
}
// Signs in the user using an ID token
suspend fun signInWithIdToken(idTokenRequest: IDTokenRequest) {
supabase.auth.signInWith(IDToken) {
idToken = idTokenRequest.idToken
provider = idTokenRequest.provider
nonce = idTokenRequest.nonce
accessToken = idTokenRequest.accessToken
}
}
// Checks if the user is signed in
fun isSignedIn(): Boolean {
return supabase.auth.currentSessionOrNull() != null
}
} 2. AppUseCaseThe class AppUseCase(private val repository: AppRepository) {
// Observe the session status as a StateFlow
fun sessionState(): StateFlow<SessionStatus> {
return repository.sessionStatus()
}
} 3. SharedViewModelThe open class SharedViewModel<S>(initialState: S) {
private val viewModelScope = CoroutineScope(Dispatchers.Main + SupervisorJob())
private val _state = MutableStateFlow(initialState)
val state: StateFlow<S> = _state.asStateFlow()
fun launch(block: suspend CoroutineScope.() -> Unit) {
viewModelScope.launch { block() }
}
fun updateState(reducer: S.() -> S) {
_state.value = _state.value.reducer()
}
fun clear() {
viewModelScope.coroutineContext.cancel()
}
} 4. AppViewModelThe class AppViewModel(
private val useCase: AppUseCase
) : SharedViewModel<AppState>(AppState()) {
init {
sessionStatus()
}
// Observe session status changes and update the UI
private fun sessionStatus() {
launch {
useCase.sessionState().collectLatest { sessionStatus ->
val authState = when (sessionStatus) {
is SessionStatus.Authenticated -> {
println("User authenticated.")
AuthState.AUTHENTICATED
}
is SessionStatus.Initializing -> {
println("Session initializing.")
AuthState.INITIALIZING
}
is SessionStatus.RefreshFailure -> {
println("Session refresh failed: ${sessionStatus.cause}")
AuthState.REFRESH_FAILURE
}
is SessionStatus.NotAuthenticated -> {
if (sessionStatus.isSignOut) {
println("User signed out.")
} else {
println("User not signed in.")
}
AuthState.NOT_AUTHENTICATED
}
}
updateState { copy(authState = authState) }
}
}
}
} Steps to Reproduce:
Troubleshooting:
Questions:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Have you tried out printing the session status directly after signing out / signing? Then we would see if the value was at least changed. Maybe it's a collecting coroutine issue |
Beta Was this translation helpful? Give feedback.
No problem! I haven’t responded in a while, and I’m not actively working on this project at the moment because I don’t have the time. But today, I noticed that my service wasn’t a singleton, which likely caused the coroutine to not function properly. Thanks for your help—it’s working now!