feat: handle 401 responses globally with axios interceptor (Closes #473) - #598
Open
waterWang wants to merge 2 commits into
Open
feat: handle 401 responses globally with axios interceptor (Closes #473)#598waterWang wants to merge 2 commits into
waterWang wants to merge 2 commits into
Conversation
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:payd_auth_tokenfrom localStoragesonner/loginafter a short delay (toast renders first)isRedirectingguard to prevent duplicate redirects on concurrent 401sKey Design Decisions
sonnertoast directly (not the React context-baseduseNotification) because the axios interceptor runs outside the React component treewindow.location.hreffor redirect (not React Router'snavigate) for the same reasonisRedirectingflag prevents multiple concurrent 401s from causing multiple redirectssonneris already a project dependency and used inNotificationProvider.tsxVerification
sonneris listed inpackage.json(v2.0.7)NotificationProvider.tsxCloses #473