fix(android): signing out ends the server session, not only the local one (#8108)
signOut cleared the session cookie before calling /api/auth/logout, so the logout went without the cookie that names the session to drop, and the server session lived on until it expired. The app still shows signed-out at once; the logout now goes first with the cookie, bounded to 5 s and run in the application scope so leaving Settings can't cancel it, and the cookie is cleared after it, whether it worked or not. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withTimeoutOrNull
|
||||
import kotlinx.serialization.json.Json
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.create
|
||||
@@ -107,18 +108,35 @@ class AuthController @Inject constructor(
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the local session immediately and best-effort hits
|
||||
* `/api/auth/logout` so the server can drop its session row.
|
||||
* Network errors are swallowed — the user is already locally
|
||||
* signed out and a stale server session times out on its own.
|
||||
* Signs out here at once and ends the session on the server too.
|
||||
*
|
||||
* The app shows signed-out straight away, then calls
|
||||
* `/api/auth/logout` while the session cookie is still set: the cookie
|
||||
* is what tells the server which session to drop, so the call has to go
|
||||
* before the cookie is cleared. The call waits at most
|
||||
* [LOGOUT_TIMEOUT_MS]; offline, or on an error, the cookie is cleared
|
||||
* anyway and the server session expires on its own. It runs in the
|
||||
* application scope, so the screen that asked closing as the app shows
|
||||
* signed-out can't cancel it halfway.
|
||||
*/
|
||||
suspend fun signOut() {
|
||||
currentUserState.value = null
|
||||
authStore.setSessionCookie(null)
|
||||
authStore.setUserJson(null)
|
||||
runCatching { api.logout() }
|
||||
scope.launch {
|
||||
try {
|
||||
withTimeoutOrNull(LOGOUT_TIMEOUT_MS) { runCatching { api.logout() } }
|
||||
} finally {
|
||||
authStore.setSessionCookie(null)
|
||||
}
|
||||
}.join()
|
||||
}
|
||||
|
||||
private fun decodeUser(raw: String): UserRef? =
|
||||
runCatching { json.decodeFromString(UserRef.serializer(), raw) }.getOrNull()
|
||||
|
||||
private companion object {
|
||||
// Long enough for a logout on a slow connection, short enough that
|
||||
// signing out offline doesn't seem to hang.
|
||||
const val LOGOUT_TIMEOUT_MS = 5_000L
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user