Skip to content

[FE-42] Set up fetch-based API client with JWT handling and auto refresh-on-401 #913

Description

@mftee

Overview

There is no API client of any kind in the frontend yet — app/ contains only a placeholder layout and page. Every subsequent page needs a single, consistent way to call the backend.

Tech

Use the native fetch API wrapped in a small typed client. No axios/TanStack Query — neither is currently installed, and this scope doesn't need them.

Acceptance Criteria

  • Create lib/api-client.ts exporting a request(path, options) helper that reads NEXT_PUBLIC_API_URL, attaches the JWT from the auth context (depends on FE-43) as a Bearer header, and parses JSON responses
  • On a 401 response, attempt a silent refresh via POST /auth/refresh once; if refresh also fails, clear the session and redirect to /login
  • Typed error handling: non-2xx responses throw a typed ApiError with status and message extracted from the backend's error shape
  • Unit tests (jest, already configured) covering success, 401-then-refresh-success, and 401-then-refresh-failure paths

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions