fix(dashboard): fix auth routing - reactive state, beforeLoad guard, post-login nav#503
Conversation
|
🔍 Reviewing PR... |
nhopeatall
left a comment
There was a problem hiding this comment.
LGTM — solid fix for the auth routing flash. The beforeLoad guard with ensureQueryData is the correct TanStack Router pattern for route-level auth gating, eliminating the post-paint blank screen.
Key things I verified:
-
invalidateQueries→ensureQueryDatahandshake: Login invalidates the cache (marks stale), thennavigatetriggersbeforeLoadwhich callsensureQueryData— since the data is stale, it refetches. This correctly guarantees fresh auth state before layout renders. -
replace: trueprevents back-button-to-login — correct. -
retry: falseon bothbeforeLoadand theuseQuerycall — consistent and eliminates the ~2s retry delay on 401s. The defaultretry: 1fromquery-client.tswould otherwise kick in. -
Reactive pathname:
useRouterState().location.pathnamecorrectly re-renders on client-side navigation, fixing the stalewindow.location.pathname. -
Logout flow (
header.tsx):queryClient.clear()+navigate({ to: '/login' })→beforeLoadearly-returns for/loginpath → no redirect loop. Works correctly. -
Mid-session token expiry: With the
meQuery.isErrorblock removed, an expired token during an active session would show the layout with undefined user data rather than redirecting. This is acceptable because (a) session cookies have 30-day expiry making this unlikely, (b)OrgProvider,Sidebar, andHeaderall handleundefineduser data gracefully, and (c) the next navigation would triggerbeforeLoadand redirect to login. The old behavior ofreturn null(blank screen) was worse.
Summary
window.location.pathnamewithuseRouterState().location.pathnamesoRootLayoutre-renders correctly on client-side navigationbeforeLoadguard on the root route that callsensureQueryDatafor auth check, throwingredirect({ to: '/login' })on failure — eliminates the post-paintuseEffectredirect and blank-screen flashuseEffectauth redirect and theif (meQuery.isError) return nullblock fromRootLayout—beforeLoadhandles this before the component renderspendingComponentto root route so a loading state is shown during asyncbeforeLoadretry: falseonauth.mequery to eliminate the ~2-second retry delay on 401 responseslogin.tsx: useinvalidateQueries(notrefetchQueries) thennavigate({ to: '/', replace: true })— thebeforeLoadguard'sensureQueryDatacall will refetch the now-stale query, guaranteeing fresh auth state before layout renders;replace: trueprevents the back button returning to the login pageTest plan
/— login page appears immediately (no blank screen)npm test -- --run)npm run lint)npx tsc --noEmitinweb/)Trello card: https://trello.com/c/cgjDU9rz/89-theres-some-weird-issue-with-dashboard-when-opening-it-for-the-first-time-it-does-not-show-the-login-screen-it-shows-only-after
🤖 Generated with Claude Code