Skip to content

feat(auth): add resetStatusOnBackground config to keep session status on background - #1339

Open
PierreVieira wants to merge 1 commit into
supabase-community:masterfrom
PierreVieira:feat/auth-reset-status-on-background
Open

feat(auth): add resetStatusOnBackground config to keep session status on background#1339
PierreVieira wants to merge 1 commit into
supabase-community:masterfrom
PierreVieira:feat/auth-reset-status-on-background

Conversation

@PierreVieira

Copy link
Copy Markdown
Contributor

Closes #1338

Problem

On Android, the lifecycle observer resets Auth.sessionStatus to SessionStatus.Initializing when the app moves to the background (onStop in Auth/src/androidMain/.../setupPlatform.kt), before it settles back to Authenticated on resume. Any consumer that renders UI from sessionStatus therefore observes a transient Initializing after every resume, which causes flicker — e.g. an authenticated user briefly appears as "loading"/"signed out".

This is Android-only: appleMain / jvmMain / webMain / linuxMain / mingwMain setupPlatform() just call initDone() and never reset to Initializing on background, so the status stays Authenticated across background/resume on those targets. The only existing opt-out, enableLifecycleCallbacks = false, is too coarse — it also disables pausing auto-refresh on background and reloading/refreshing the session on foreground.

Change

Add an opt-out config flag (additive, default = current behavior, no breaking change):

install(Auth) {
    resetStatusOnBackground = false
}
  • true (default): unchanged — status is reset to Initializing on background.
  • false: the last resolved status (e.g. Authenticated) is kept while auto-refresh is still paused on background; loadFromStorage() / refresh on resume continue to work exactly as before.

The onStop reset is now routed through an internal Auth.resetSessionStatusForBackground() helper (in Utils.kt, next to initDone()), so the behavior is unit-testable without driving ProcessLifecycleOwner.

Tests

Added two tests in AuthTest:

  • default keeps current behavior (background → Initializing);
  • with resetStatusOnBackground = false, the status stays Authenticated after backgrounding.

Notes

… on background

On Android, the lifecycle observer resets `Auth.sessionStatus` to
`SessionStatus.Initializing` when the app moves to the background (`onStop`),
before settling back to `Authenticated` on resume. Consumers that render UI from
`sessionStatus` therefore observe a transient `Initializing` after every resume,
causing flicker (e.g. an authenticated user briefly appears as loading). This is
Android-only: the other platforms never reset to `Initializing` on background.

Add an opt-out `resetStatusOnBackground` config flag (default `true`, no behavior
change). When `false`, the last resolved status is kept while auto-refresh is still
paused on background; `loadFromStorage()`/refresh on resume keep working as before.

The reset is now done via an internal `Auth.resetSessionStatusForBackground()`
helper, covered by tests in `AuthTest`.

Closes supabase-community#1338

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature request]: Option to keep session status when app is backgrounded (don't reset to Initializing)

1 participant