Skip to content

fix(auth): auto-redirect to login on 401 when JWT expires - #331

Merged
javi11 merged 1 commit into
mainfrom
fix/401-redirect-to-login
Feb 25, 2026
Merged

fix(auth): auto-redirect to login on 401 when JWT expires#331
javi11 merged 1 commit into
mainfrom
fix/401-redirect-to-login

Conversation

@javi11

@javi11 javi11 commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Emit a custom api:unauthorized window event whenever any API call returns HTTP 401 (in both request() and requestWithMeta() in APIClient)
  • AuthProvider listens for the event and dispatches AUTH_LOGOUT when the user is currently authenticated — causing ProtectedRoute to render <LoginPage /> immediately
  • Hides the logout button in UserMenu when loginRequired === false (auth-disabled mode)
  • Returns an anonymous admin user from GET /api/user when auth is disabled, so the endpoint works in no-auth mode

Problem solved

When the JWT expired, polling queries (e.g. dashboard stats every 5 s) started returning 401. The frontend had no global 401 handler, so each page showed an <ErrorAlert> ("Authentication required") while isAuthenticated remained true. The user was stuck.

Test plan

  • Log in with login_required: true
  • Expire the JWT (wait or delete the cookie in DevTools)
  • Next polling request triggers 401 → app automatically shows the login page
  • After re-logging in, normal operation resumes
  • With auth disabled (login_required: false), no logout button is shown and the user menu still works
  • bun run check passes with no TypeScript errors

🤖 Generated with Claude Code

- Emit custom window event `api:unauthorized` on any 401 response in
  APIClient (both request() and requestWithMeta())
- AuthProvider listens for the event and dispatches AUTH_LOGOUT when
  the user is supposedly authenticated, causing ProtectedRoute to
  render LoginPage immediately instead of showing error alerts
- Hide logout button in UserMenu when login is not required
- Return anonymous admin user from /api/user endpoint when auth is
  disabled so no-auth mode works without a logged-in user

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@javi11
javi11 merged commit 50de967 into main Feb 25, 2026
1 check passed
@javi11
javi11 deleted the fix/401-redirect-to-login branch February 26, 2026 14:08
yoshitaka420 pushed a commit to yoshitaka420/altmount that referenced this pull request Jun 1, 2026
)

- Emit custom window event `api:unauthorized` on any 401 response in
  APIClient (both request() and requestWithMeta())
- AuthProvider listens for the event and dispatches AUTH_LOGOUT when
  the user is supposedly authenticated, causing ProtectedRoute to
  render LoginPage immediately instead of showing error alerts
- Hide logout button in UserMenu when login is not required
- Return anonymous admin user from /api/user endpoint when auth is
  disabled so no-auth mode works without a logged-in user

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
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