Auth: load auth on app start and fix storage when token is null to prevent intermittent logged-out state

This commit is contained in:
2025-10-08 23:11:08 +02:00
parent 38301efccb
commit e81abae275
2 changed files with 18 additions and 6 deletions

View File

@@ -174,8 +174,14 @@ fun KlottrAppShell(
val currentLoc = locService.location.collectAsState(initial = null)
val scope = rememberCoroutineScope()
// Säkerställ att auth återställs från plattforms-lagring vid uppstart
LaunchedEffect(Unit) {
app.klottr.util.AuthShared.loadFromStorage()
}
// Inloggning
val loggedIn by AuthShared.loggedIn.collectAsState(initial = false)
val loggedIn by AuthShared.loggedIn.collectAsState(initial = app.klottr.util.AuthShared.loggedIn.value)
// Initiera SQLDelight-databasen med hjälp av DatabaseFactory.
val db = remember {

View File

@@ -26,12 +26,18 @@ object AuthShared {
idToken = token
userId = sub
email = mail
_loggedIn.value = token != null
val isLogged = token != null
_loggedIn.value = isLogged
_email.value = mail
if (token != null) {
if (isLogged) {
PlatformAuthStorage.set(KEY_IDTOKEN, token)
PlatformAuthStorage.set(KEY_SUB, sub)
PlatformAuthStorage.set(KEY_EMAIL, mail)
} else {
// Viktigt: rensa lagringen om vi inte har en giltig token
PlatformAuthStorage.clear()
}
}