fix: démarrage immédiat + session conservée hors-ligne
- AuthViewModel: affiche l'app immédiatement si un token existe, sans attendre le refresh réseau — le refresh + sync se font en arrière-plan - BonsaiAuthManager.refreshIfNeeded: ne déconnecte l'utilisateur que sur 401/403 (token invalide), pas sur erreur réseau ou timeout Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -16,7 +16,7 @@ android {
|
|||||||
minSdk = 26
|
minSdk = 26
|
||||||
targetSdk = 35
|
targetSdk = 35
|
||||||
versionCode = 1
|
versionCode = 1
|
||||||
versionName = "0.0.18"
|
versionName = "0.0.19"
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -97,12 +97,23 @@ class BonsaiAuthManager @Inject constructor(
|
|||||||
|
|
||||||
runCatching {
|
runCatching {
|
||||||
httpClient.newCall(request).execute().use { response ->
|
httpClient.newCall(request).execute().use { response ->
|
||||||
if (!response.isSuccessful) return@withContext false
|
when {
|
||||||
val raw = response.body?.string() ?: return@withContext false
|
response.isSuccessful -> {
|
||||||
|
val raw = response.body?.string() ?: return@withContext true
|
||||||
saveTokens(JSONObject(raw))
|
saveTokens(JSONObject(raw))
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}.getOrDefault(false)
|
response.code == 401 || response.code == 403 -> {
|
||||||
|
// Refresh token genuinely invalid — must re-login.
|
||||||
|
false
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
// Server error or network issue — keep existing token, don't logout.
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.getOrDefault(true) // Network exception (offline, timeout) → keep existing token.
|
||||||
}
|
}
|
||||||
|
|
||||||
fun logout() {
|
fun logout() {
|
||||||
|
|||||||
@@ -38,12 +38,12 @@ class AuthViewModel @Inject constructor(
|
|||||||
_status.value = AuthStatus.NotAuthenticated
|
_status.value = AuthStatus.NotAuthenticated
|
||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
if (authManager.refreshIfNeeded()) {
|
// Show the app immediately — don't block on a network call.
|
||||||
_status.value = AuthStatus.Authenticated(authManager.getUsername())
|
_status.value = AuthStatus.Authenticated(authManager.getUsername())
|
||||||
|
// Refresh token + sync in background without blocking startup.
|
||||||
|
viewModelScope.launch {
|
||||||
|
authManager.refreshIfNeeded()
|
||||||
syncManager.sync()
|
syncManager.sync()
|
||||||
} else {
|
|
||||||
authManager.logout()
|
|
||||||
_status.value = AuthStatus.NotAuthenticated
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user