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 currentLoc = locService.location.collectAsState(initial = null)
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
// Säkerställ att auth återställs från plattforms-lagring vid uppstart
LaunchedEffect(Unit) {
app.klottr.util.AuthShared.loadFromStorage()
}
// Inloggning // 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. // Initiera SQLDelight-databasen med hjälp av DatabaseFactory.
val db = remember { val db = remember {

View File

@@ -24,14 +24,20 @@ object AuthShared {
/** Called by platform code after a successful sign-in (or silent restore). */ /** Called by platform code after a successful sign-in (or silent restore). */
fun applyLogin(token: String?, sub: String?, mail: String?) { fun applyLogin(token: String?, sub: String?, mail: String?) {
idToken = token idToken = token
userId = sub userId = sub
email = mail email = mail
_loggedIn.value = token != null
_email.value = mail val isLogged = token != null
if (token != null) { _loggedIn.value = isLogged
_email.value = mail
if (isLogged) {
PlatformAuthStorage.set(KEY_IDTOKEN, token) PlatformAuthStorage.set(KEY_IDTOKEN, token)
PlatformAuthStorage.set(KEY_SUB, sub) PlatformAuthStorage.set(KEY_SUB, sub)
PlatformAuthStorage.set(KEY_EMAIL, mail) PlatformAuthStorage.set(KEY_EMAIL, mail)
} else {
// Viktigt: rensa lagringen om vi inte har en giltig token
PlatformAuthStorage.clear()
} }
} }