Skip to content

feat(frontend): implement centralized axios API client and global 401 handling#142

Open
ranbeer06052009 wants to merge 7 commits into
Team-NoxVeil:mainfrom
ranbeer06052009:feat/centralized-api-client
Open

feat(frontend): implement centralized axios API client and global 401 handling#142
ranbeer06052009 wants to merge 7 commits into
Team-NoxVeil:mainfrom
ranbeer06052009:feat/centralized-api-client

Conversation

@ranbeer06052009
Copy link
Copy Markdown

Resolves #137
@sathwikshetty33 This PR refactors the frontend network architecture by replacing raw fetch calls with a centralized axios API client. It significantly reduces code duplication and improves security/UX by handling token expirations gracefully at a global level.

Changes Made

  • Added Axios Dependency: Installed axios to handle HTTP requests.
  • Created apiClient.ts:
    • Added a Request Interceptor that automatically injects the Authorization: Bearer <token> header into all outgoing API requests (prioritizing token then org_token from localStorage).
    • Added a Response Interceptor that globally catches 401 Unauthorized responses, clears stale authentication data, and safely redirects the user to the /login page.
  • Refactored Services: Migrated auth.service.ts, user.service.ts, interview.service.ts, and organization.service.ts to use apiClient instead of fetch.
  • Removed Token Prop Drilling: Since the token is now injected globally, the token argument was safely removed from all service functions. Consequently, the token prop was cleanly removed from the entire React component tree (App.tsx, DashboardPage.tsx, InterviewSessionPage.tsx, ProfileSetupPage.tsx, etc.) and their respective hooks.

Testing Performed

  • ✅ Executed full TypeScript compilation (npm run build / tsc -b) with 0 errors.
  • ✅ Verified apiClient automatically attaches the JWT header to outgoing requests.
  • ✅ Verified that if a 401 error is thrown by the backend, the global interceptor successfully catches it, clears storage, and redirects to /login.

Type of Change

  • Refactor (code improvement that doesn't change external behavior)
  • Feature (new non-breaking feature)
  • Bug fix (non-breaking change which fixes an issue)

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the frontend networking layer from raw fetch calls scattered across service files to a centralized axios instance (apiClient) with a request interceptor that injects the bearer token from localStorage and a response interceptor that clears auth state and redirects to /login on 401. Service signatures lose their token parameter, and that prop is removed throughout the component/hook tree.

Changes:

  • Adds apiClient.ts with request (token injection) and response (global 401 handling) interceptors; bumps axios to ^1.16.1.
  • Migrates auth.service.ts, user.service.ts, interview.service.ts, and organization.service.ts from fetch to apiClient, dropping the token argument on each function.
  • Removes token prop drilling from App.tsx, DashboardPage, InterviewSessionPage, ProfileSetupPage, DsaPanel, and the corresponding hooks (useDashboard, useInterviewSession, useProfileSetup).

Reviewed changes

Copilot reviewed 14 out of 15 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
frontend/src/services/apiClient.ts New axios instance with request/response interceptors for token injection and 401 redirect.
frontend/src/services/auth.service.ts Switches loginUser/fetchCurrentUser to apiClient; keeps BASE_URL only for Google OAuth URL.
frontend/src/services/user.service.ts Replaces fetch helpers with apiClient; drops token params.
frontend/src/services/interview.service.ts Replaces fetch helpers with apiClient; drops token params on all DSA/session calls.
frontend/src/services/organization.service.ts Replaces fetch with apiClient for org signup/login.
frontend/src/features/user/ProfileSetupPage.tsx Removes token prop; converts component to function declaration.
frontend/src/features/user/hooks/useProfileSetup.ts Drops token parameter and adjusts updateUserProfile calls.
frontend/src/features/user/hooks/useDashboard.ts Drops token parameter and effect dependency; removes early-return guard.
frontend/src/features/user/DashboardPage.tsx Removes token prop and threading into useDashboard.
frontend/src/features/interview/InterviewSessionPage.tsx Removes token prop and removes it from renderRound/DsaPanel.
frontend/src/features/interview/hooks/useInterviewSession.ts Drops token from hook signature and effect deps.
frontend/src/features/interview/components/DsaPanel.tsx Removes token prop; simplifies DSA service call sites.
frontend/src/App.tsx Removes token props passed down to pages; updates fetchCurrentUser call.
frontend/package.json, package-lock.json Bumps axios to ^1.16.1 (pulls in https-proxy-agent, agent-base).
Files not reviewed (1)
  • frontend/package-lock.json: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread frontend/src/services/apiClient.ts Outdated
Comment thread frontend/src/services/apiClient.ts
Comment thread frontend/src/services/apiClient.ts Outdated
@ranbeer06052009
Copy link
Copy Markdown
Author

@RITVIKKAMASETTY can you please rereview if any further changes needed

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.

Frontend Architecture: Implement Centralized API Client & Global 401 Handling

2 participants