feat(client): add cookie-backed authentication UI

- Add session-aware login, logout and protected navigation.
- Send cookie credentials for API and run-event requests with localized UI stories.
This commit is contained in:
agent committed 2026-11-05 04:18:11 -05:00
1 parent 9e4b692444
commit 92141a25fb
26 files changed
+1191 -12

No files matched your search

+54 -4
View File
@@ -31,7 +31,25 @@ const BASE_URL = ((import.meta.env.VITE_API_URL as string | undefined) ?? '/api/
/** URL of the server-sent events stream of run lifecycle events. */
export const RUN_EVENTS_URL = `${BASE_URL}/scenarios/runs/events`;
async function request<T>(path: string, init?: RequestInit): Promise<T> {
export class ApiError extends Error {
constructor(
public readonly status: number,
message: string,
) {
super(message);
this.name = 'ApiError';
}
}
interface RequestOptions {
quietErrors?: boolean;
}
async function request<T>(
path: string,
init?: RequestInit,
options: RequestOptions = {},
): Promise<T> {
let res: Response;
try {
const headers = new Headers(init?.headers);
@@ -41,18 +59,26 @@ async function request<T>(path: string, init?: RequestInit): Promise<T> {
res = await fetch(`${BASE_URL}${path}`, {
...init,
credentials: init?.credentials ?? 'include',
headers,
});
} catch (err) {
const message = (err as Error).message || 'Network request failed';
emitApiErrorToast(message);
if (!options.quietErrors) emitApiErrorToast(message);
throw new Error(message);
}
if (!res.ok) {
const text = await res.text().catch(() => res.statusText);
const message = `${res.status} ${text}`;
emitApiErrorToast(message);
throw new Error(message);
const error = new ApiError(res.status, message);
if (res.status === 401 && !path.startsWith('/auth/')) {
const event = new CustomEvent('liqa:unauthorized', { cancelable: true });
const handled = !window.dispatchEvent(event);
if (!handled && !options.quietErrors) emitApiErrorToast(message);
} else if (!options.quietErrors && !path.startsWith('/auth/')) {
emitApiErrorToast(message);
}
throw error;
}
const contentLength = res.headers.get('content-length');
if (res.status === 204 || contentLength === '0') return undefined as T;
@@ -65,6 +91,30 @@ async function request<T>(path: string, init?: RequestInit): Promise<T> {
return text as T;
}
export interface AuthUser {
id: string;
username: string;
createdAt: string;
updatedAt: string;
}
export const auth = {
async login(username: string, password: string): Promise<AuthUser> {
const response = await request<{ user: AuthUser }>(
'/auth/login',
{ method: 'POST', body: JSON.stringify({ username, password }) },
{ quietErrors: true },
);
return response.user;
},
me(): Promise<AuthUser> {
return request('/auth/me', undefined, { quietErrors: true });
},
logout(): Promise<void> {
return request('/auth/logout', { method: 'POST' }, { quietErrors: true });
},
};
// ── Credentials ───────────────────────────────────────────────────────────────
export const credentials = {