Skip to content

Commit

Permalink
Merge pull request #459 from supabase-community/fix-android-crash
Browse files Browse the repository at this point in the history
Change lifecycle callbacks on Android and add previous status to SessionStatus.Authenticated
  • Loading branch information
jan-tennert authored Feb 6, 2024
2 parents b1e0ec5 + 04fa612 commit ef117de
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ private fun addLifecycleCallbacks(gotrue: Auth) {
override fun onStart(owner: LifecycleOwner) {
if(!gotrue.isAutoRefreshRunning && gotrue.config.alwaysAutoRefresh) {
Logger.d("Auth") {
"Starting auto refresh"
"Trying to re-load session from storage..."
}
scope.launch {
try {
gotrue.startAutoRefreshForCurrentSession()
} catch(e: IllegalStateException) {
val sessionFound = gotrue.loadFromStorage()
if(!sessionFound) {
Logger.d("Auth") {
"No session found for auto refresh"
"No session found, not starting auto refresh"
}
} else {
Logger.d("Auth") {
"Session found, auto refresh started"
}
}
}
Expand All @@ -56,6 +59,7 @@ private fun addLifecycleCallbacks(gotrue: Auth) {
Logger.d("Auth") { "Cancelling auto refresh because app is switching to the background" }
scope.launch {
gotrue.stopAutoRefreshForCurrentSession()
gotrue.resetLoadingState()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ internal class AuthImpl(
val session = currentSessionOrNull() ?: return
val newUser = session.user?.copy(identities = session.user.identities?.filter { it.identityId != identityId })
val newSession = session.copy(user = newUser)
_sessionStatus.value = SessionStatus.Authenticated(newSession)
_sessionStatus.value = SessionStatus.Authenticated(newSession, sessionStatus.value)
}
}

Expand Down Expand Up @@ -214,7 +214,7 @@ internal class AuthImpl(
if (this.config.autoSaveToStorage) {
sessionManager.saveSession(newSession)
}
_sessionStatus.value = SessionStatus.Authenticated(newSession)
_sessionStatus.value = SessionStatus.Authenticated(newSession, sessionStatus.value)
}
return userInfo
}
Expand Down Expand Up @@ -350,7 +350,7 @@ internal class AuthImpl(
val user = retrieveUser(currentAccessTokenOrNull() ?: error("No session found"))
if (updateSession) {
val session = currentSessionOrNull() ?: error("No session found")
val newStatus = SessionStatus.Authenticated(session.copy(user = user))
val newStatus = SessionStatus.Authenticated(session.copy(user = user), sessionStatus.value)
_sessionStatus.value = newStatus
if (config.autoSaveToStorage) sessionManager.saveSession(newStatus.session)
}
Expand Down Expand Up @@ -395,7 +395,7 @@ internal class AuthImpl(

override suspend fun importSession(session: UserSession, autoRefresh: Boolean) {
if (!autoRefresh) {
_sessionStatus.value = SessionStatus.Authenticated(session)
_sessionStatus.value = SessionStatus.Authenticated(session, sessionStatus.value)
if (session.refreshToken.isNotBlank() && session.expiresIn != 0L && config.autoSaveToStorage) {
sessionManager.saveSession(session)
}
Expand All @@ -407,7 +407,7 @@ internal class AuthImpl(
{ importSession(session) }
)
} else {
_sessionStatus.value = SessionStatus.Authenticated(session)
_sessionStatus.value = SessionStatus.Authenticated(session, sessionStatus.value)
if (config.autoSaveToStorage) sessionManager.saveSession(session)
sessionJob?.cancel()
sessionJob = authScope.launch {
Expand Down Expand Up @@ -541,6 +541,10 @@ internal class AuthImpl(
sessionStatus.first { it !is SessionStatus.LoadingFromStorage }
}

fun resetLoadingState() {
_sessionStatus.value = SessionStatus.LoadingFromStorage
}

}

@SupabaseInternal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.jan.supabase.gotrue

import io.github.jan.supabase.gotrue.user.UserSession
import kotlin.jvm.JvmInline

/**
* Represents the status of the current session in [Auth]
Expand All @@ -26,7 +25,7 @@ sealed interface SessionStatus {
/**
* This status means that [Auth] holds a valid session
* @param session The session
* @param oldStatus The previous status. Useful for knowing if the user was already authenticated
*/
@JvmInline
value class Authenticated(val session: UserSession) : SessionStatus
data class Authenticated(val session: UserSession, val oldStatus: SessionStatus) : SessionStatus
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ kotlin.experimental.tryK2=false
org.jetbrains.compose.experimental.uikit.enabled=true
org.jetbrains.compose.experimental.jscanvas.enabled=true

supabase-version = 2.1.2
supabase-version = 2.1.3

0 comments on commit ef117de

Please sign in to comment.