@aaronlew-suite/auth-client
v0.1.2
Published
The browser half of suite auth: AuthProvider, useAuth, apiFetch, route guards (ARCHITECTURE §3.1, §2.2).
Readme
@aaronlew-suite/auth-client
The browser half of suite auth (ARCHITECTURE §2.2, §3.3). The SPA never implements auth: it wraps
itself in <AuthProvider> and talks to its API through apiFetch. There are no tokens in the
browser to handle — only an HttpOnly session cookie the API set.
Published on public npm (registry.npmjs.org): installing needs no token or .npmrc entry.
import { AuthProvider, RequireAuth, useAuth, api } from '@aaronlew-suite/auth-client';
<AuthProvider>
<RequireAuth fallback={<Spinner />}>
<App />
</RequireAuth>
</AuthProvider>;
const { user, signOut, hasGroup } = useAuth();
const items = await api.get<Page<Item>>('/api/v1/items');What it does for you — each item is the contract kit-auth enforces server-side
(platform/templates/webapp-requirements.md §3):
| | |
| ---------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| apiFetch / api.* | Relative /api URLs, cookies included, X-Requested-With: fetch on POST/PUT/PATCH/DELETE (the CSRF middleware 403s without it), Idempotency-Key on request |
| Errors | problem+json → UnauthorizedError, ForbiddenError, NotFoundError, ConflictError, ValidationError, ApiError; NetworkError when no response arrived. Each carries requestId — show it in error UI |
| 401 | A full-page navigation to the login path in the body, with ?next= the current path. Never a fetch: the navigation is what makes silent SSO work (§2.3.2) |
| useAuth() | status, user (from /api/auth/me), login(), signOut(), refresh(), hasGroup() |
| signOut() | POST /api/auth/logout, then navigate to end_session_url — or reload if it is null (§2.3.4) |
| <RequireAuth> / <RequireGroup> | Route guards. Group gating here is presentation only; the API's require_group is the real check |
| subscribe() | Server-sent events with the session cookie |
Movies is the one cross-origin app (§2.4):
configureApi({ baseUrl: 'https://api.movies.aaronlew.com' }) switches to credentials: 'include'.
