Skip to content

fix(frontend): mark auth ready only after the session check settles (#360) - #361

Merged
karutoil merged 2 commits into
mainfrom
pullfrog/360-auth-ready-after-session-check
Nov 17, 2026
Merged

karutoil merged 2 commits into
mainfrom
pullfrog/360-auth-ready-after-session-check

Conversation

@pullfrog

@pullfrog pullfrog Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Problem

refresh() opened with set({ isRefreshing: true, error: null, isReady: true }), so the auth store announced "auth decided" before /api/auth/me answered. App gates the router on isReady, ProtectedRoute redirects to /login while isAuthenticated is still false, and LoginPage redirects back once the probe lands — the login-page flash reported in #360. Every background caller inherited it: the profile poll, the SSE user_updated / permissions_updated handlers and the profile mutations.

Changes

stores/authStore.ts

  • refresh() never writes isReady. An in-flight counter lets only the last overlapping probe clear isRefreshing.
  • init() owns the boot transition: it awaits refresh() and flips isReady in finally, so no guard observes an unresolved session.
  • A transient probe failure still keeps a live session and now sets a translated error; when it is the boot probe it also sets sessionCheckFailed, so App shows a retry screen instead of a bare /login (and a retry that fails the same way does not dismiss it).
  • The 401 sign-out branch clears the privileged query cache, since the interceptor no longer does it for /api/auth/me.
  • The loginGuard short-circuit leaves readiness to login().

hooks/useAuthInit.ts

  • No longer sets isReady. 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 with sessionCheckFailed, which App renders as a retry screen whose button calls init().

App.tsx

  • The 20s boot backstop no longer overrides an in-flight session probe and re-arms instead of disabling itself.
  • New retry screen for sessionCheckFailed, with the retry button disabled while the probe runs. It mounts the auth routes and links to /login, so a persistently failing /api/auth/me cannot trap a signed-out user away from the sign-in form.

services/api/client.ts

  • The global 401 handler now skips auth-state and cache clearing for public endpoints (/api/auth/me, /api/setup/status, /api/theme-settings/public), not just the redirect. The store's refresh() is now the single place that decides sign-out, which is what useProfileSync's "the poll must not wipe the session" contract always assumed.

Tests

  • New stores/__tests__/authStore.test.ts: readiness only after the probe settles (success, 401, transient), refresh() alone never sets isReady, transient failures keep the session and set the error, boot failure flags the retry screen and survives a failed retry, overlapping init()/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/me delayed 2s) recording pushState/replaceState paths, asserting the app never navigates through /login.

Notes / deviations from the plan

  • The message key is common:sessionCheck.failed rather than an auth: key: common is bundled eagerly, so the store can resolve it synchronously at boot, before the lazy auth namespace has loaded (the setupStatus.* precedent is the same).
  • The plan assumed /api/auth/me was 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.
  • useProfileSync already used authApi.refresh() plus a display-fields patch, so it needed no change; the logout() queryClient import 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:verify all 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  | View workflow run | via Pullfrog | Using pullfrog | 𝕏

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
@karutoil

Copy link
Copy Markdown
Collaborator

@pullfrog review this please

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 touches isReady; an in-flight counter lets only the last overlapping probe clear isRefreshing; init() flips readiness in finally so no guard observes an unresolved session; a transient boot failure keeps a live session but raises sessionCheckFailed; 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 with sessionCheckFailed rather 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 reads sessionCheckFailed.
  • services/api/client.ts — the global 401 handler no longer clears auth state or the query cache for /api/auth/me, /api/setup/status or /api/theme-settings/public; the store is the single place that decides sign-out.
  • Coverage — new authStore and apiClient unit tests, two Playwright reload guards that record pushState/replaceState paths, and sessionCheck.failed added to all four catalogs (sorted and translated correctly).

Ran the full frontend suite here: 693 tests pass, including both new spec files.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using pullfrog | 𝕏

Comment thread catalyst-frontend/src/App.tsx Resolved
@pullfrog

pullfrog Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Addressed the review: the sessionCheckFailed gate in catalyst-frontend/src/App.tsx now mounts the auth routes (/login, /register, /forgot-password, /reset-password, /two-factor) with the retry screen as the catch-all, plus a "Sign in" link on the screen. Added src/__tests__/App.sessionCheck.test.tsx covering both the retry screen and the link to the login form; frontend suite now 695 tests passing, with typecheck, lint and i18n checks clean. Pushed as f4bcd56 and resolved the thread.

Task list (1/4 completed)
  • Read the review thread and verify the finding against App.tsx
  • Fix the retry screen trapping signed-out users away from auth routes
  • Run frontend typecheck/lint/tests on changed code
  • Commit, push, reply to thread, resolve, report

Pullfrog  | View workflow run | via Pullfrog | Using pullfrog | 𝕏

@karutoil
karutoil merged commit faa0b43 into main Nov 17, 2026
13 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant