diff --git a/frontend/src/services/api.ts b/frontend/src/services/api.ts index 4479995..918dea8 100644 --- a/frontend/src/services/api.ts +++ b/frontend/src/services/api.ts @@ -25,49 +25,13 @@ api.interceptors.request.use((config) => { return config }) -// On 401 responses, attempt one silent token refresh. If that also -// fails, clear stored credentials so the AuthContext falls back to the -// login screen on its next render. -let isRefreshing = false -let refreshSubscribers: ((token: string) => void)[] = [] - -api.interceptors.response.use( - (response) => response, - async (error) => { - const original = error.config - if (error.response?.status !== 401 || original._retry) { - return Promise.reject(error) - } - - // Skip retry for auth endpoints themselves to avoid loops. - if (original.url?.startsWith('/auth/')) { - return Promise.reject(error) - } - - original._retry = true - - if (!isRefreshing) { - isRefreshing = true - // The refresh token lives in AuthContext memory, not in - // localStorage. The interceptor can't access it directly, so we - // rely on the AuthContext's scheduled refresh to keep the access - // token fresh. If the access token is truly expired and no - // refresh has happened, we just force a logout. - localStorage.removeItem('access_token') - isRefreshing = false - // Reject — AuthContext will detect the missing token and show login. - return Promise.reject(error) - } - - // Another request is already refreshing — queue this one. - return new Promise((resolve) => { - refreshSubscribers.push((token: string) => { - original.headers.Authorization = `Bearer ${token}` - resolve(api(original)) - }) - }) - }, -) +// 401 handling lives in AuthContext.tsx, which mounts a response +// interceptor that swaps an expired access_token via /auth/refresh and +// retries the original request. We deliberately don't register a +// competing interceptor here — an earlier version did, and it set +// `original._retry = true` and nuked localStorage before AuthContext's +// interceptor could run, so every 401 forced a logout instead of a +// silent refresh. // Source Folders API. Source roots are config-driven now (PHOTO_DIRS in // .env → bootstrap on backend startup), so the UI only reads them and