Skip to content

refactor(api-client): transparent 401 -> refresh -> retry-once#491

Open
Agbasimere wants to merge 1 commit into
Remitwise-Org:mainfrom
Agbasimere:refactor/apiclient-401-refresh-retry
Open

refactor(api-client): transparent 401 -> refresh -> retry-once#491
Agbasimere wants to merge 1 commit into
Remitwise-Org:mainfrom
Agbasimere:refactor/apiclient-401-refresh-retry

Conversation

@Agbasimere

@Agbasimere Agbasimere commented Jun 17, 2026

Copy link
Copy Markdown

Description

This PR addresses the issue of spurious failures when a user's iron-session expires mid-flow. It ensures that the client attempts a transparent session refresh followed by a single retry before gracefully falling back to the standard session-expiry UI.

Fixes #484

Changes Made:

  • Refresh Endpoint: Added POST /api/auth/refresh that utilizes getSessionWithRefresh() to safely slide the session window.
  • Session Handler: Introduced sessionHandler.refreshSession() which incorporates a shared, deduplicated promise. This guarantees that concurrent 401 responses don't result in parallel refresh requests.
  • API Client: Intercepts 401 Unauthorized responses in apiClient.ts to attempt a background refresh. If the refresh succeeds, the client retries the failed request immediately with an _isRetry internal flag to avoid infinite loops.
  • Hook & Page Integration: Converted direct fetch() calls in useFormAction.ts, app/dashboard/transaction-history/page.tsx, and app/settings/page.tsx to leverage the enhanced apiClient.
  • Testing: Implemented comprehensive Vitest unit tests (tests/session/apiClient.test.ts) validating 200 fast-paths, 401 refresh+retry successes, 401 refresh failures, and non-retry cases like 403. Tokens remain securely out of logs.

Centralizes authenticated fetches, refreshes the session on 401 and
retries once, falling back to the session-expiry path on failure.
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.

Centralize and retry-handle session-aware fetches in lib/client/apiClient.ts (401 -> refresh -> retry)

1 participant