Skip to content

feat: handle 401 responses globally with axios interceptor (Closes #473) - #598

Open
waterWang wants to merge 2 commits into
Protocol-Guild:mainfrom
waterWang:feat/473-401-interceptor
Open

feat: handle 401 responses globally with axios interceptor (Closes #473)#598
waterWang wants to merge 2 commits into
Protocol-Guild:mainfrom
waterWang:feat/473-401-interceptor

Conversation

@waterWang

Copy link
Copy Markdown

Summary

Adds a global axios response interceptor to handle 401 (Unauthorized) responses across the entire application. Previously, expired tokens caused silent failures with spinners or empty data — now users get a clear toast notification and are redirected to login.

Changes

  • frontend/src/utils/api.ts: Added response interceptor that:
    • Detects 401 status from any API call
    • Clears payd_auth_token from localStorage
    • Shows a toast: "Session expired, please log in again" via sonner
    • Redirects to /login after a short delay (toast renders first)
    • Uses isRedirecting guard to prevent duplicate redirects on concurrent 401s

Key Design Decisions

  • Uses sonner toast directly (not the React context-based useNotification) because the axios interceptor runs outside the React component tree
  • Uses window.location.href for redirect (not React Router's navigate) for the same reason
  • The isRedirecting flag prevents multiple concurrent 401s from causing multiple redirects
  • sonner is already a project dependency and used in NotificationProvider.tsx

Verification

  • sonner is listed in package.json (v2.0.7)
  • Pattern matches existing usage in NotificationProvider.tsx
  • All existing API calls continue to work — the interceptor is purely additive

Closes #473

- Convert all 20 page imports to React.lazy() dynamic imports
- Add Suspense wrapper with loading spinner (Loader2 from lucide-react)
- Keep layout/utility components (EmployerLayout, ErrorBoundary,
  ErrorFallback) as regular imports since they are not route pages
- Each page chunk loads on demand, reducing initial bundle size

Closes Protocol-Guild#469
- Add axios response interceptor for 401 status
- Clear auth token from localStorage on 401
- Show toast notification via sonner: 'Session expired, please log in again'
- Redirect to /login with guard to prevent duplicate redirects
- Uses existing sonner dependency already in the project

Closes Protocol-Guild#473
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.

Handle 401 responses globally with axios interceptor

1 participant