fix: centralize API base URL across all frontend services (Closes #479) - #594
Open
waterWang wants to merge 2 commits into
Open
fix: centralize API base URL across all frontend services (Closes #479)#594waterWang wants to merge 2 commits into
waterWang wants to merge 2 commits into
Conversation
Add a global axios response interceptor so expired sessions never surface as silent spinners or empty tables. On 401 (non-auth endpoints) the interceptor: clears the payd_auth_token from localStorage, shows a sonner toast 'Session expired, please log in again', and redirects to /login. Registers on both the shared api instance and bare axios calls, with guards for /auth endpoints and redirect loops. Closes Protocol-Guild#473
Replace 18 hardcoded localhost fallback URLs with a single shared API_BASE_URL constant from utils/api.ts. Every service file now: - Uses the shared axios instance (with unified base URL, auth token interceptor, and 401 response interceptor) instead of raw with per-file VITE_API_URL or VITE_API_BASE_URL - Removes per-file API_BASE_URL constants and authHeaders() helpers - Uses relative paths that work with the shared base URL Key changes: - utils/api.ts exports API_BASE_URL and API_ROOT_URL as single source of truth, configured via VITE_API_URL env var (fallback localhost:4000/api) - 15 service files migrated to the shared api instance - SocketProvider and Login page use API_ROOT_URL for socket/OAuth URLs - Renamed VITE_API_BASE_URL refs to VITE_API_URL (certificateApi, cashFlowForecastApi, contracts.ts) - Removed authHeaders() functions from 3 files (the shared api instance auto-adds the Bearer token via request interceptor) Closes Protocol-Guild#479
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
Centralizes all API base URL configuration across the frontend. Previously, 18 different hardcoded
localhostfallback URLs (:3000,:3001,:4000,/api,/api/v1, etc.) were scattered across service files, causing silent failures when any non-local deployment pointed to the wrong port. This is the root cause of why mock data appears — API calls fail and silently fallback to mocks.Changes
Single source of truth (
frontend/src/utils/api.ts)API_BASE_URL(viaVITE_API_URLenv var, fallbackhttp://localhost:4000/api)API_ROOT_URL(strips/apisuffix for endpoints outside the API prefix)apiaxios instance and the 401 response interceptor now use these constants15 service files migrated to shared
apiinstanceanchor.tsaxios.get+localhost:3000/apiapi.get('/payments/...')auditApi.tsaxios.get+localhost:3001/apiapi.get('/audit')benefitsApi.tsaxios.get+localhost:3001/api/v1+authHeaders()api.get('/v1/benefits/...')cashFlowForecastApi.tsaxios.get+VITE_API_BASE_URL+localhost:3001+ manualAuthorizationapi.get('/cash-flow/...')certificateApi.tsaxios.get+VITE_API_BASE_URL+localhost:3001api.get('/certificates/...')contracts.tsaxios.get+VITE_API_BASE_URL+localhost:3000api.get('/contracts')forecastApi.tsaxios.get+localhost:3001/api/v1api.get('/v1/forecast/...')pathfinding.tsaxios.get+localhost:3000/apiapi.get('/payments/paths')scheduleApi.tsaxios.get+localhost:3001/api/v1api.get('/v1/schedules')taxComplianceApi.tsaxios.get+localhost:3001/api/v1+authHeaders()api.get('/v1/taxes/...')webhookApi.tsaxios.get+localhost:4000/api/v1+authHeaders()api.get(WEBHOOKS_URL)withdrawal.tsaxios.get+localhost:3000/apiapi.get('/withdrawal/...')Non-service files
SocketProvider.tsx— now usesAPI_ROOT_URLinstead ofVITE_API_URL || 'localhost:3000'Login.tsx— now usesAPI_ROOT_URLinstead ofVITE_BACKEND_URL || 'localhost:4000'Removed
authHeaders()functions from 3 files (sharedapiinstance auto-addsBearertoken)VITE_API_BASE_URLenv var — all references changed toVITE_API_URLTesting
tsc --noEmitpasses)VITE_API_URLused everywherelocalhostURLs in service filesapiinstance handles auth token + 401 interceptor globallyCloses #479