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,114 @@
|
||||
import '../../src/i18n';
|
||||
import type { Meta, StoryObj } from '@storybook/react-vite';
|
||||
import { expect, userEvent, within } from 'storybook/test';
|
||||
import { ApiError } from '../../src/api/client';
|
||||
import { LoginForm } from '../../src/pages/auth/LoginForm';
|
||||
|
||||
const meta: Meta<typeof LoginForm> = {
|
||||
title: 'Authentication/Login form',
|
||||
component: LoginForm,
|
||||
parameters: { layout: 'centered' },
|
||||
tags: ['autodocs'],
|
||||
args: { onSubmit: async () => undefined },
|
||||
decorators: [
|
||||
(Story) => (
|
||||
<div style={{ width: 'min(calc(100vw - 32px), 440px)' }}>
|
||||
<Story />
|
||||
</div>
|
||||
),
|
||||
],
|
||||
};
|
||||
export default meta;
|
||||
type Story = StoryObj<typeof LoginForm>;
|
||||
|
||||
export const Initial: Story = {};
|
||||
|
||||
export const ExpiredSession: Story = { args: { expired: true } };
|
||||
|
||||
export const SignedOut: Story = { args: { signedOut: true } };
|
||||
|
||||
export const DarkTheme: Story = {
|
||||
decorators: [
|
||||
(Story) => (
|
||||
<div data-theme="dark" style={{ width: 440, padding: 24, background: 'var(--color-bg)' }}>
|
||||
<Story />
|
||||
</div>
|
||||
),
|
||||
],
|
||||
};
|
||||
|
||||
export const NarrowViewport: Story = {
|
||||
decorators: [
|
||||
(Story) => (
|
||||
<div style={{ width: 320, padding: 16, background: 'var(--color-bg)' }}>
|
||||
<Story />
|
||||
</div>
|
||||
),
|
||||
],
|
||||
};
|
||||
|
||||
export const EmptyFieldValidation: Story = {
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.click(canvas.getByRole('button', { name: 'Sign in' }));
|
||||
await expect(canvas.getByText('Enter your username.')).toBeVisible();
|
||||
await expect(canvas.getByRole('textbox', { name: 'Username' })).toHaveFocus();
|
||||
await expect(canvas.getByLabelText('Password')).toHaveAttribute('aria-invalid', 'true');
|
||||
},
|
||||
};
|
||||
|
||||
export const InvalidCredentials: Story = {
|
||||
args: {
|
||||
onSubmit: async () => {
|
||||
throw new ApiError(401, 'Unauthorized');
|
||||
},
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.type(canvas.getByRole('textbox', { name: 'Username' }), 'operator');
|
||||
await userEvent.type(canvas.getByLabelText('Password'), 'wrong-password');
|
||||
await userEvent.click(canvas.getByRole('button', { name: 'Sign in' }));
|
||||
await expect(await canvas.findByRole('alert')).toHaveTextContent(
|
||||
'Username or password is incorrect.',
|
||||
);
|
||||
await expect(canvas.getByRole('textbox', { name: 'Username' })).toHaveValue('operator');
|
||||
await expect(canvas.getByLabelText('Password')).toHaveValue('');
|
||||
await expect(canvas.getByLabelText('Password')).toHaveFocus();
|
||||
},
|
||||
};
|
||||
|
||||
export const RequestFailureAndRetry: Story = {
|
||||
args: {
|
||||
onSubmit: (() => {
|
||||
let attempts = 0;
|
||||
return async () => {
|
||||
attempts += 1;
|
||||
if (attempts === 1) throw new ApiError(503, 'Unavailable');
|
||||
};
|
||||
})(),
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.type(canvas.getByRole('textbox', { name: 'Username' }), 'operator');
|
||||
await userEvent.type(canvas.getByLabelText('Password'), 'temporary');
|
||||
await userEvent.click(canvas.getByRole('button', { name: 'Sign in' }));
|
||||
await expect(await canvas.findByRole('alert')).toHaveTextContent(
|
||||
"Couldn't sign in. Check your connection and try again.",
|
||||
);
|
||||
await expect(canvas.getByLabelText('Password')).toHaveValue('temporary');
|
||||
await userEvent.click(canvas.getByRole('button', { name: 'Sign in' }));
|
||||
await expect(canvas.queryByRole('alert')).not.toBeInTheDocument();
|
||||
},
|
||||
};
|
||||
|
||||
export const Submitting: Story = {
|
||||
args: { onSubmit: () => new Promise(() => undefined) },
|
||||
play: async ({ canvasElement }) => {
|
||||
const canvas = within(canvasElement);
|
||||
await userEvent.type(canvas.getByRole('textbox', { name: 'Username' }), 'operator');
|
||||
await userEvent.type(canvas.getByLabelText('Password'), 'secret');
|
||||
await userEvent.click(canvas.getByRole('button', { name: 'Sign in' }));
|
||||
await expect(canvas.getByRole('status')).toHaveTextContent('Signing in…');
|
||||
await expect(canvas.getByRole('button', { name: 'Signing in…' })).toBeDisabled();
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user