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:
1 parent
9e4b692444
commit
92141a25fb
26 files changed
+1191
-12
No files matched your search
@@ -0,0 +1,71 @@
|
||||
import type { Meta, StoryObj } from '@storybook/react-vite';
|
||||
import { expect } from 'storybook/test';
|
||||
import { auth } from '../../src/api/client';
|
||||
import { createRunEventSource } from '../../src/hooks/useRunNotifications';
|
||||
|
||||
function TransportExample() {
|
||||
return <p>Browser auth uses HttpOnly cookies for API and run-event requests.</p>;
|
||||
}
|
||||
|
||||
const meta: Meta<typeof TransportExample> = {
|
||||
title: 'Authentication/Cookie transport',
|
||||
component: TransportExample,
|
||||
tags: ['autodocs'],
|
||||
};
|
||||
export default meta;
|
||||
type Story = StoryObj<typeof TransportExample>;
|
||||
|
||||
export const CookieCredentials: Story = {
|
||||
play: async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
const localStorageLength = localStorage.length;
|
||||
let requestInit: RequestInit | undefined;
|
||||
globalThis.fetch = async (_input, init) => {
|
||||
requestInit = init;
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
user: {
|
||||
id: 'synthetic-user-id',
|
||||
username: 'synthetic-operator',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
}),
|
||||
{ status: 200, headers: { 'Content-Type': 'application/json' } },
|
||||
);
|
||||
};
|
||||
try {
|
||||
const user = await auth.login('synthetic-operator', 'synthetic-password');
|
||||
await expect(requestInit?.credentials).toBe('include');
|
||||
await expect(user.username).toBe('synthetic-operator');
|
||||
await expect(new Headers(requestInit?.headers).has('Authorization')).toBe(false);
|
||||
await expect(localStorage.length).toBe(localStorageLength);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export const EventStreamIncludesCookieCredentials: Story = {
|
||||
play: async () => {
|
||||
const previous = Object.getOwnPropertyDescriptor(globalThis, 'EventSource');
|
||||
let options: EventSourceInit | undefined;
|
||||
class StubEventSource {
|
||||
constructor(_url: string | URL, init?: EventSourceInit) {
|
||||
options = init;
|
||||
}
|
||||
close() {}
|
||||
}
|
||||
Object.defineProperty(globalThis, 'EventSource', {
|
||||
configurable: true,
|
||||
value: StubEventSource,
|
||||
});
|
||||
try {
|
||||
createRunEventSource().close();
|
||||
await expect(options?.withCredentials).toBe(true);
|
||||
} finally {
|
||||
if (previous) Object.defineProperty(globalThis, 'EventSource', previous);
|
||||
else Reflect.deleteProperty(globalThis, 'EventSource');
|
||||
}
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user