feat(auth): add resetStatusOnBackground config to keep session status on background - #1339
Open
PierreVieira wants to merge 1 commit into
Open
Conversation
… 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>
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1338
Problem
On Android, the lifecycle observer resets
Auth.sessionStatustoSessionStatus.Initializingwhen the app moves to the background (onStopinAuth/src/androidMain/.../setupPlatform.kt), before it settles back toAuthenticatedon resume. Any consumer that renders UI fromsessionStatustherefore observes a transientInitializingafter every resume, which causes flicker — e.g. an authenticated user briefly appears as "loading"/"signed out".This is Android-only:
appleMain/jvmMain/webMain/linuxMain/mingwMainsetupPlatform()just callinitDone()and never reset toInitializingon background, so the status staysAuthenticatedacross 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):
true(default): unchanged — status is reset toInitializingon 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
onStopreset is now routed through an internalAuth.resetSessionStatusForBackground()helper (inUtils.kt, next toinitDone()), so the behavior is unit-testable without drivingProcessLifecycleOwner.Tests
Added two tests in
AuthTest:Initializing);resetStatusOnBackground = false, the status staysAuthenticatedafter backgrounding.Notes
enableLifecycleCallbacks = true.CHANGELOG.mdsince there's no unreleased section — happy to add an entry wherever you prefer.Initializingon background (making Android consistent with the other targets). I went with an opt-out flag to avoid any behavior change by default, but happy to switch approaches if you'd rather.