Skip to content

Login page briefly appears when refreshing an authenticated session #360

Closed

Description

@Correlander

Describe the bug

When I refresh the panel while already logged in, I sometimes briefly get shown the login page before being sent straight back to the panel.

It seems like the panel might be deciding where to send me before it's actually finished checking whether I'm logged in. It's not a major issue on its own, but ideally I shouldn't be seeing the login page at all if my session is still valid.

To Reproduce

  1. Log in to the Catalyst panel.
  2. Navigate to a page like /servers or /dashboard.
  3. Refresh the tab.
  4. The login page may briefly appear before the panel loads normally.

Expected behavior

Refreshing with a valid session should keep me on the page I was on. The panel should finish checking the existing session before deciding whether to show the login page or continue to the panel.

Desktop

  • OS: Fedora Linux
  • Browser: Zen

Additional context

I had a look through the authentication code and noticed something that might explain this.

In authStore.ts, refresh() sets isReady: true before awaiting the session check:

refresh: async () => {
  set({ isRefreshing: true, error: null, isReady: true });
  try {
    const { user } = await authApi.refresh();

What's readable is that init() even has a comment saying not to mark authentication as ready until refresh completes, but refresh() itself appears to do exactly that.

Looking at how the rest fits together:

  • App.tsx uses isReady as part of its initial loading check.
  • ProtectedRoute.tsx redirects to /login if isReady is true but isAuthenticated is false.
  • LoginPage.tsx redirects authenticated users back to the panel.

So I'm wondering if there's a window where the panel considers authentication ready, sees that I'm not authenticated yet, sends me to login, and then sends me straight back once the session check finishes.

I can't say for certain that's what's happening without tracing it at runtime, but the code seems to allow that sequence.

I also noticed that useAuthInit.ts has a timeout that can independently mark authentication ready, while client.ts can reset authentication state on 401 responses.

It might be worth looking at how these interact, especially when the backend is slow or requests overlap. I'd be more concerned about inconsistent session handling than the brief login-page flash itself.

From what I could see, protected routes still check authentication and permissions, so I haven't found anything suggesting an authorization bypass, if that is a concern of yours (since I am bringing up authorization state race conditions).

Ideally, the initial session check should finish before the panel makes a decision about where to send the user, rather than potentially bouncing through login first.

Activity

  1. pullfrog commented on Nov 16, 2026

    @pullfrog
    Contributor

    Diagnosis

    The reporter's read of the code is correct, and there is a second, worse path they did not find.

    refresh() opens with set({ isRefreshing: true, error: null, isReady: true }) (catalyst-frontend/src/stores/authStore.ts:162). App.tsx:213 gates the whole router on isReady, ProtectedRoute renders a spinner until isReady and then <Navigate to="/login"> when isAuthenticated is false, and LoginPage redirects back once authentication lands. So the store is announcing "auth decided" before it has decided anything, which is exactly the bounce the issue describes.

    Every caller of refresh() outside boot inherits that write: useProfileSync (polls /api/auth/me on mount and every 60 s), useSseAdminEvents and useServerStateUpdates (on user_updated / permissions_updated), and ProfilePage (after profile, avatar and password mutations). On a real-time page a single SSE event can therefore remount the router while the session is momentarily unresolved — the "more concerned about inconsistent session handling than the flash" part of the report.

    Three things amplify it:

    • The transient branch demotes a live session. For a 429/408/5xx/network failure the catch block sets isRefreshing: false, isReady: true but leaves user and isAuthenticated alone. LoginPage only redirects when isAuthenticated is true, so a background refresh that hits a 500 while the user sits on /login (or is bounced there by something else) strands an authenticated user on the sign-in form. Failures also set no error, so nothing is visible either.
    • The isReady backdoor in useAuthInit and App. The 12 s init timeout (useAuthInit.ts:14) and the 20 s boot backstop (App.tsx:122) both flip isReady while the session probe is still in flight, with no way for the late refresh() to undo a premature false.
    • Two isReady writers with no ordering. The storage listener in authStore.ts:322 calls init() on every cross-tab catalyst-auth write, and logout() re-writes catalyst-auth, so a tab can run overlapping refresh() calls (deduped at the request layer, not the store layer) whose finally sets isRefreshing: false while another is still running.

    Good news for correctness: client.ts bypasses the global 401 handling for /api/auth/me, so a noisy session probe cannot itself clear state. This is a bad route decision, not an authorization bypass — ProtectedRoute still checks permissions after isReady, so a session that resolves after an early /login bounce simply gets redirected back.

    Plan

    1. Make isReady mean "the session question is answered" (authStore.ts)

    • refresh() no longer writes isReady. Add a module-level in-flight counter in createAuthState's closure; the finally block clears isRefreshing and sets isReady: true only when no other refresh is in flight (last one wins).
    • init() keeps sole ownership of the boot transition: await refresh() in a try/catch, then set isReady: true in finally.
    • On transient failure, keep the session as-is and set error (getLocalizedErrorMessage(err)) so LoginPage can explain the state instead of showing a blank form. Add a dedicated translated key (e.g. auth:errors.sessionCheckFailed) rather than reusing a generic one; run pnpm --filter catalyst-frontend run i18n:extract and fill fr + zh-CN in the same change.
    • loginGuard.active short-circuit: keep the session and return without marking ready (boot is owned by login, whose own isReady: true is correct there).

    2. Tighten the timeout backdoors (useAuthInit.ts, App.tsx)

    • 12 s init timeout: instead of setting isReady, re-run the session probe once (capped retry). If it still does not settle, keep the current fail-open (isReady: true) but route to a "couldn't check your session — retry" state rather than a bare /login; the retry reuses init(). This is the path a slow backend takes, so it is where the flash is most likely in practice.
    • 20 s App backstop: only flip bootTimedOut when both isReady is false and no refresh is in flight, or drop the flag for auth entirely — it currently duplicates useAuthInit's job.
    • Keep useSetupStatus's fast path untouched; it already routes immediately when install memory exists.

    3. Stop remounting the router from background refreshes

    Audit the four refresh() call sites (useProfileSync, useSseAdminEvents:248, useServerStateUpdates:302, ProfilePage). Once step 1 lands they no longer move isReady, but they should not need to touch isRefreshing either — useProfileSync in particular can switch to a display-fields-only patch (it already has syncDisplayFieldsToStore) and drop its authApi.refresh() round trip entirely.

    Also verify the await import('../lib/queryClient') inside logout() — queryClient.ts:56 re-exports the class, so check the import resolves to the singleton instance and clear the right client.

    4. Regression tests (vitest, src/stores/__tests__/authStore.test.ts, new file)

    Fake timers plus a stubbed authApi.refresh:

    • refresh() alone never sets isReady — the exact assert on the reported line.
    • init() sets isReady only after the probe settles, for success, 401 and transient failure.
    • A transient failure keeps isAuthenticated: true, sets error, and leaves isReady untouched when called post-boot.
    • Two overlapping init() / refresh() calls: isReady flips exactly once and the last settle wins.
    • Global 401 interceptor test in services/api/__tests__/client.test.ts: a 401 on /api/auth/me does not clear state.

    5. E2E guard (catalyst-frontend/e2e/functional.spec.ts, Authentication Flows)

    After login(page), assert the URL never passes through /login on a reload: install a page.on('framenavigated') recorder before page.goto('/servers') and expect the final URL to be /servers with no /login entry. Repeat with /api/auth/me delayed via page.route (e.g. 2 s) to force the slow-backend window — this is the scenario the issue reports and the one a plain reload will not reproduce reliably.

    6. Verify and ship

    • pnpm --filter catalyst-frontend run lint, cd catalyst-frontend && npx tsc --noEmit, pnpm --filter catalyst-frontend exec vitest run, pnpm i18n:check, pnpm i18n:hardcoded, pnpm --filter catalyst-frontend run i18n:verify.
    • Per AGENTS.md, check it in the browser: sign in, reload on /servers and /dashboard with a throttled backend, and confirm no /login paint.
    • Docs live in the separate catalyst-doc repo; note the session-bootstrap behaviour there if a page covers auth/session handling.
    • Commit as fix(frontend): don't mark auth ready before the session check settles, to prevent the login-page flash on reload (closes #360) and land via push to main unless a PR is requested.

    Out of scope

    No backend change is needed — /api/auth/me already caches its response for 5 s (catalyst-backend/src/routes/auth.ts:35) and is exempt from the global 401 handling on the client.

    Pullfrog  | Implement plan ➔ | View workflow run | via Pullfrog | Using pullfrog | 𝕏

  2. added
    area:frontend
    bug
    on Nov 16, 2026
  3. pullfrog commented on Nov 17, 2026

    @pullfrog
    Contributor

    Implemented the plan and opened #361.

    refresh() no longer writes isReady (an in-flight counter gates isRefreshing); init() is the only boot writer. A transient probe failure keeps the session and sets a translated error, and a boot probe that fails or never settles now leads to a retry screen in App instead of a bare /login. The 12s hook deadline re-arms once before failing open, and the 20s App backstop no longer overrides an in-flight probe.

    Two corrections to the plan's premises: /api/auth/me was not exempt from the global 401 handling (only from the redirect — it still cleared state and cache), so the interceptor now skips state/cache clearing for public endpoints and the store's refresh() owns sign-out and cache clearing; and useProfileSync already used authApi.refresh() with a display-fields patch, so it needed no change.

    Regression tests cover the store's readiness ordering, overlapping probes, failed retries and the probe's 401, plus two Playwright reload guards (one with /api/auth/me delayed 2s) that fail if the app ever navigates through /login. tsc, lint, the full vitest suite (693 tests) and all i18n checks pass; a live browser check was not possible here (no database, docker daemon or Playwright browsers), so that path relies on the new e2e specs.

    Task list (1/8 completed)
    • Read authStore, useAuthInit, App, callers, tests to understand current behaviour
    • Step 1: make refresh() not write isReady; init() owns it; transient failure sets error + i18n key
    • Step 2: tighten useAuthInit 12s timeout + App 20s backstop
    • Step 3: audit refresh() call sites; drop useProfileSync round trip; verify logout queryClient import
    • Step 4: regression tests (authStore + client 401)
    • Step 5: E2E guard in functional.spec.ts
    • Step 6: run lint, tsc, vitest, i18n checks
    • Self-review with reviewfrog, commit, push, open PR

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:frontendbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions