Admin: gebruikerslijst blijft actueel, sessie blijft behouden bij tijdelijke refresh-fouten

- Refresh wist de sessie alleen nog bij 400/401/403; 429/5xx/netwerkfout behoudt hem (opstart: rustig opnieuw proberen met backoff, anders gast zonder het cookie te raken)
- Refresh is single-flight (opstart, 401-interceptor, profiel delen een lopende aanvraag)
- GET-verzoeken met cache: no-store; gebruikerslijst: refetch bij focus en poll 45 s; sessie ververst als de eigen rol in de lijst afwijkt

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 13:32:46 +01:00
co-authored by Claude Sonnet 5.5
parent 2cdcb9d868
commit 2f3f7fbb2f
11 changed files with 299 additions and 15 deletions
+32
View File
@@ -211,3 +211,35 @@ describe('ApiClient', () => {
});
});
});
describe('ApiClient 401 interceptor with a refresh that fails for a transient reason', () => {
it('does not call the auth failure handler and passes on the original 401', async () => {
server.use(http.get(`${BASE}/protected-transient`, () => new HttpResponse(null, { status: 401 })));
const client = makeClient();
const failureFn = vi.fn();
client.setRefreshHandler(async () => {
throw new Error('rate limited');
});
client.setAuthFailureHandler(failureFn);
await expect(client.get('/protected-transient')).rejects.toThrow(ProblemDetailsError);
expect(failureFn).not.toHaveBeenCalled();
});
it('asks for GET responses straight from the server (no cache)', async () => {
let cacheMode: string | undefined;
const original = globalThis.fetch;
globalThis.fetch = ((input: RequestInfo | URL, init?: RequestInit) => {
cacheMode = init?.cache;
return original(input, init);
}) as typeof fetch;
server.use(http.get(`${BASE}/nocache`, () => HttpResponse.json({})));
try {
await makeClient().get('/nocache');
} finally {
globalThis.fetch = original;
}
expect(cacheMode).toBe('no-store');
});
});
+12 -2
View File
@@ -99,10 +99,18 @@ export class ApiClient {
// 401 intercept: try a single cookie-based refresh, then retry once (BR-U1-04).
if (response.status === 401 && !options.skipAuthRefresh && this.refreshHandler !== null) {
const newToken = await this.refreshHandler();
let newToken: string | null = null;
let rejected = false;
try {
newToken = await this.refreshHandler();
rejected = newToken === null;
} catch {
// Transient (rate limit, restart, network): the session is not known to be gone, so it is not
// cleared; the caller gets the original 401.
}
if (newToken !== null) {
response = await this.rawFetch(path, options);
} else {
} else if (rejected) {
this.authFailureHandler?.();
}
}
@@ -126,6 +134,8 @@ export class ApiClient {
...rest,
headers: finalHeaders,
credentials: 'include',
// A list read after a change must come from the server, never from an intermediate cache.
...(rest.method === 'GET' ? { cache: 'no-store' as const } : {}),
body: body !== undefined && body !== null ? JSON.stringify(body) : undefined,
});
} catch (cause) {
+13
View File
@@ -0,0 +1,13 @@
import { ProblemDetailsError } from '@/lib/api-client';
/** Waits (ms) between the quiet start-up retries of the silent refresh; tests shorten this. */
export const refreshBackoff = { delays: [400, 1200, 2500] as number[] };
/**
* True when the SERVER says the refresh cookie is not (or no longer) valid: 400, 401 or 403. Anything else -
* a rate limit (429), a restart (5xx, 502 from the proxy) or a network error - says nothing about the session,
* so the session must not be dropped for it.
*/
export function isAuthRejection(error: unknown): boolean {
return error instanceof ProblemDetailsError && (error.status === 400 || error.status === 401 || error.status === 403);
}