Repository navigation
fix(frontend): mark auth ready only after the session check settles (#360) - #361
Conversation
refresh() announced isReady before the /api/auth/me probe answered, so App remounted the router and ProtectedRoute bounced authenticated reloads to /api/login (the login-page flash). refresh() no longer touches isReady; init() owns the boot transition and an in-flight counter lets the last overlapping probe own isRefreshing. - Transient probe failures keep the session and now set a translated error; a failed boot probe marks sessionCheckFailed so App shows a retry screen instead of a bare /login. - useAuthInit re-arms its 12s deadline once, then fails open with the retry flag; App's 20s backstop no longer overrides an in-flight probe. - The global 401 handler stops clearing auth state and the query cache for public endpoints (/api/auth/me, setup status, public theme); the store's refresh() decides sign-out and clears the cache there. - Regression tests for the store and the probe's 401, plus Playwright guards that a reload (also with a 2s-delayed probe) never navigates through /login. Closes #360
|
@pullfrog review this please |
There was a problem hiding this comment.
Important
The new retry screen is a hard gate in front of the router, so a persistently failing /api/auth/me leaves signed-out users with no route to the sign-in form.
Approach: sound — readiness now comes from the session answer itself rather than from entering the probe, and sign-out moves into the store, so every background caller inherits the fix instead of each one working around the interceptor.
Reviewed changes
isReady is redefined as "the session question is answered": only the boot probe, the flows that establish or end a session, and a 401 from a protected call may write it.
stores/authStore.ts—refresh()never touchesisReady; an in-flight counter lets only the last overlapping probe clearisRefreshing;init()flips readiness infinallyso no guard observes an unresolved session; a transient boot failure keeps a live session but raisessessionCheckFailed; the 401 branch clears the privileged query cache now that the interceptor no longer does it for/api/auth/me.hooks/useAuthInit.ts— the 12s deadline re-arms once instead of declaring "no session", and after 24s fails open withsessionCheckFailedrather than a bare sign-out.App.tsx— the 20s boot backstop now defers while a session probe is in flight (re-arming instead of overriding it, which was the flash) and reports once; the new retry screen readssessionCheckFailed.services/api/client.ts— the global 401 handler no longer clears auth state or the query cache for/api/auth/me,/api/setup/statusor/api/theme-settings/public; the store is the single place that decides sign-out.- Coverage — new
authStoreandapiClientunit tests, two Playwright reload guards that recordpushState/replaceStatepaths, andsessionCheck.failedadded to all four catalogs (sorted and translated correctly).
Ran the full frontend suite here: 693 tests pass, including both new spec files.
pullfrog | 𝕏
|
Addressed the review: the Task list (1/4 completed)
|

Problem
refresh()opened withset({ isRefreshing: true, error: null, isReady: true }), so the auth store announced "auth decided" before/api/auth/meanswered.Appgates the router onisReady,ProtectedRouteredirects to/loginwhileisAuthenticatedis still false, andLoginPageredirects back once the probe lands — the login-page flash reported in #360. Every background caller inherited it: the profile poll, the SSEuser_updated/permissions_updatedhandlers and the profile mutations.Changes
stores/authStore.tsrefresh()never writesisReady. An in-flight counter lets only the last overlapping probe clearisRefreshing.init()owns the boot transition: it awaitsrefresh()and flipsisReadyinfinally, so no guard observes an unresolved session.error; when it is the boot probe it also setssessionCheckFailed, so App shows a retry screen instead of a bare/login(and a retry that fails the same way does not dismiss it)./api/auth/me.loginGuardshort-circuit leaves readiness tologin().hooks/useAuthInit.tsisReady. The 12s deadline is re-armed once (joining the in-flight probe, or starting a fresh one if it already failed); after 24s it fails open withsessionCheckFailed, which App renders as a retry screen whose button callsinit().App.tsxsessionCheckFailed, with the retry button disabled while the probe runs. It mounts the auth routes and links to/login, so a persistently failing/api/auth/mecannot trap a signed-out user away from the sign-in form.services/api/client.ts/api/auth/me,/api/setup/status,/api/theme-settings/public), not just the redirect. The store'srefresh()is now the single place that decides sign-out, which is whatuseProfileSync's "the poll must not wipe the session" contract always assumed.Tests
stores/__tests__/authStore.test.ts: readiness only after the probe settles (success, 401, transient),refresh()alone never setsisReady, transient failures keep the session and set the error, boot failure flags the retry screen and survives a failed retry, overlappinginit()/refresh()settle once, loginGuard short-circuit.services/api/__tests__/client.test.ts: a 401 from the session probe leaves a live session alone.src/__tests__/App.sessionCheck.test.tsx: the retry screen renders for app routes and its sign-in link reaches the login form.e2e/functional.spec.ts: two reload guards (plain and with/api/auth/medelayed 2s) recordingpushState/replaceStatepaths, asserting the app never navigates through/login.Notes / deviations from the plan
common:sessionCheck.failedrather than anauth:key:commonis bundled eagerly, so the store can resolve it synchronously at boot, before the lazyauthnamespace has loaded (thesetupStatus.*precedent is the same)./api/auth/mewas already exempt from the global 401 handling; it was only exempt from the hard redirect and still cleared state. That is fixed rather than just tested.useProfileSyncalready usedauthApi.refresh()plus a display-fields patch, so it needed no change; thelogout()queryClientimport was verified to resolve to the singleton.Verification
npx tsc --noEmit,pnpm --filter catalyst-frontend run lint,npx vitest run(695 tests),pnpm i18n:check,pnpm i18n:hardcoded,pnpm --filter catalyst-frontend run i18n:verifyall pass. Browser verification was not possible in this environment (no database, docker daemon or Playwright browsers), so the Playwright reload guards and the new component test cover the reload and recovery paths in CI.pullfrog| 𝕏