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,57 @@
|
||||
.page {
|
||||
min-height: 100vh;
|
||||
padding: 24px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
color: var(--color-text);
|
||||
background: var(--color-bg);
|
||||
font-family: var(--font-family);
|
||||
}
|
||||
|
||||
.header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.logo {
|
||||
display: block;
|
||||
width: auto;
|
||||
height: 32px;
|
||||
padding: 3px 6px;
|
||||
background: #e6edea;
|
||||
border-radius: var(--radius-md);
|
||||
}
|
||||
|
||||
.main {
|
||||
flex: 1;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
padding: var(--space-6) 0;
|
||||
}
|
||||
|
||||
.message {
|
||||
width: min(100%, 440px);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: stretch;
|
||||
gap: var(--space-4);
|
||||
margin: 0;
|
||||
padding: var(--space-6);
|
||||
text-align: center;
|
||||
background: var(--color-bg);
|
||||
border: var(--border-width) solid var(--color-border);
|
||||
border-radius: var(--radius-lg);
|
||||
box-shadow: var(--shadow-sm);
|
||||
}
|
||||
|
||||
.message p {
|
||||
margin: 0;
|
||||
color: var(--color-error-fg);
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
.page {
|
||||
padding: 16px;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Button, ThemeSwitcher } from '../../ui';
|
||||
import { usePageTitle } from '../../hooks/usePageTitle';
|
||||
import liqaLogo from '../../assets/liqa.svg';
|
||||
import styles from './AuthStatusPage.module.css';
|
||||
|
||||
export function AuthStatusPage({
|
||||
failed = false,
|
||||
onRetry,
|
||||
}: {
|
||||
failed?: boolean;
|
||||
onRetry?: () => void;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
usePageTitle(t('auth.page_title'));
|
||||
return (
|
||||
<div className={styles.page}>
|
||||
<header className={styles.header}>
|
||||
<img src={liqaLogo} alt="Liqa" className={styles.logo} />
|
||||
<ThemeSwitcher />
|
||||
</header>
|
||||
<main className={styles.main} aria-live="polite">
|
||||
{failed ? (
|
||||
<section className={styles.message}>
|
||||
<p role="alert">{t('auth.session_check_failed')}</p>
|
||||
<Button type="button" size="lg" onClick={onRetry}>
|
||||
{t('auth.retry')}
|
||||
</Button>
|
||||
</section>
|
||||
) : (
|
||||
<p className={styles.message} role="status">
|
||||
{t('auth.checking_session')}
|
||||
</p>
|
||||
)}
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
.card {
|
||||
width: min(100%, 400px);
|
||||
padding: 24px;
|
||||
color: var(--color-text);
|
||||
background: var(--color-bg);
|
||||
border: var(--border-width) solid var(--color-border);
|
||||
border-radius: var(--radius-lg);
|
||||
box-shadow: var(--shadow-sm);
|
||||
}
|
||||
|
||||
.title {
|
||||
margin: 0 0 var(--space-5);
|
||||
font-size: var(--font-size-lg);
|
||||
line-height: 1.3;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.title:focus {
|
||||
outline: 2px solid var(--color-primary-text);
|
||||
outline-offset: 4px;
|
||||
border-radius: var(--radius-sm);
|
||||
}
|
||||
|
||||
.notice,
|
||||
.formError {
|
||||
margin: 0;
|
||||
padding: var(--space-3) var(--space-4);
|
||||
color: var(--color-error-fg);
|
||||
background: var(--color-error-bg);
|
||||
border-radius: var(--radius-md);
|
||||
font-size: var(--font-size-sm);
|
||||
}
|
||||
|
||||
.form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--space-4);
|
||||
}
|
||||
|
||||
.submit {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.status {
|
||||
margin: 0;
|
||||
color: var(--color-text-muted);
|
||||
font-size: var(--font-size-sm);
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
.card {
|
||||
padding: var(--space-4);
|
||||
border: 0;
|
||||
border-radius: 0;
|
||||
box-shadow: none;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
import { useEffect, useRef, useState, type FormEvent } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { ApiError } from '../../api/client';
|
||||
import { Button, Input } from '../../ui';
|
||||
import styles from './LoginForm.module.css';
|
||||
|
||||
export interface LoginFormProps {
|
||||
expired?: boolean;
|
||||
signedOut?: boolean;
|
||||
onSubmit: (username: string, password: string) => Promise<void>;
|
||||
}
|
||||
|
||||
export function LoginForm({ expired = false, signedOut = false, onSubmit }: LoginFormProps) {
|
||||
const { t } = useTranslation();
|
||||
const titleRef = useRef<HTMLHeadingElement>(null);
|
||||
const formRef = useRef<HTMLFormElement>(null);
|
||||
const [username, setUsername] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [usernameError, setUsernameError] = useState('');
|
||||
const [passwordError, setPasswordError] = useState('');
|
||||
const [formError, setFormError] = useState('');
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
titleRef.current?.focus();
|
||||
}, []);
|
||||
|
||||
function focusInput(name: string) {
|
||||
formRef.current?.querySelector<HTMLInputElement>(`input[name="${name}"]`)?.focus();
|
||||
}
|
||||
|
||||
async function handleSubmit(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
if (submitting) return;
|
||||
setFormError('');
|
||||
const missingUsername = username.trim().length === 0;
|
||||
const missingPassword = password.length === 0;
|
||||
setUsernameError(missingUsername ? t('auth.username_required') : '');
|
||||
setPasswordError(missingPassword ? t('auth.password_required') : '');
|
||||
if (missingUsername || missingPassword) {
|
||||
focusInput(missingUsername ? 'username' : 'password');
|
||||
return;
|
||||
}
|
||||
|
||||
setSubmitting(true);
|
||||
try {
|
||||
await onSubmit(username, password);
|
||||
} catch (error) {
|
||||
if (error instanceof ApiError && error.status === 401) {
|
||||
setFormError(t('auth.invalid_credentials'));
|
||||
setPassword('');
|
||||
focusInput('password');
|
||||
} else {
|
||||
setFormError(t('auth.sign_in_failed'));
|
||||
}
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<section className={styles.card} aria-labelledby="login-title">
|
||||
<h1 id="login-title" ref={titleRef} tabIndex={-1} className={styles.title}>
|
||||
{t('auth.sign_in')}
|
||||
</h1>
|
||||
{expired && <p className={styles.notice}>{t('auth.session_expired')}</p>}
|
||||
{signedOut && <p className={styles.notice}>{t('auth.signed_out')}</p>}
|
||||
<form
|
||||
ref={formRef}
|
||||
className={styles.form}
|
||||
onSubmit={(event) => void handleSubmit(event)}
|
||||
noValidate
|
||||
>
|
||||
<Input
|
||||
name="username"
|
||||
label={t('auth.username')}
|
||||
autoComplete="username"
|
||||
autoCapitalize="none"
|
||||
autoCorrect="off"
|
||||
value={username}
|
||||
onChange={(event) => {
|
||||
setUsername(event.target.value);
|
||||
if (usernameError) setUsernameError('');
|
||||
}}
|
||||
error={usernameError || undefined}
|
||||
disabled={submitting}
|
||||
/>
|
||||
<Input
|
||||
name="password"
|
||||
type="password"
|
||||
label={t('auth.password')}
|
||||
autoComplete="current-password"
|
||||
value={password}
|
||||
onChange={(event) => {
|
||||
setPassword(event.target.value);
|
||||
if (passwordError) setPasswordError('');
|
||||
}}
|
||||
error={passwordError || undefined}
|
||||
disabled={submitting}
|
||||
/>
|
||||
{formError && (
|
||||
<p className={styles.formError} role="alert">
|
||||
{formError}
|
||||
</p>
|
||||
)}
|
||||
{submitting && (
|
||||
<p className={styles.status} role="status" aria-live="polite">
|
||||
{t('auth.signing_in')}
|
||||
</p>
|
||||
)}
|
||||
<Button
|
||||
type="submit"
|
||||
size="lg"
|
||||
loading={submitting}
|
||||
disabled={submitting}
|
||||
className={styles.submit}
|
||||
>
|
||||
{t(submitting ? 'auth.signing_in' : 'auth.sign_in')}
|
||||
</Button>
|
||||
</form>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
.page {
|
||||
min-height: 100vh;
|
||||
padding: 24px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
color: var(--color-text);
|
||||
background: var(--color-bg);
|
||||
font-family: var(--font-family);
|
||||
}
|
||||
|
||||
.header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: var(--space-4);
|
||||
}
|
||||
|
||||
.logo {
|
||||
display: block;
|
||||
width: auto;
|
||||
height: 32px;
|
||||
padding: 3px 6px;
|
||||
background: #e6edea;
|
||||
border-radius: var(--radius-md);
|
||||
}
|
||||
|
||||
.main {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
padding: var(--space-6) 0;
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
.page {
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
.main {
|
||||
align-items: start;
|
||||
padding-top: clamp(40px, 12vh, 96px);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { useCallback } from 'react';
|
||||
import { useNavigate, useSearchParams } from 'react-router-dom';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useAuth } from '../../auth/useAuth';
|
||||
import { safeReturnTo } from '../../auth/returnTo';
|
||||
import { usePageTitle } from '../../hooks/usePageTitle';
|
||||
import { ThemeSwitcher } from '../../ui';
|
||||
import liqaLogo from '../../assets/liqa.svg';
|
||||
import { LoginForm } from './LoginForm';
|
||||
import styles from './LoginPage.module.css';
|
||||
|
||||
export function LoginPage() {
|
||||
const auth = useAuth();
|
||||
const navigate = useNavigate();
|
||||
const [search] = useSearchParams();
|
||||
const { t } = useTranslation();
|
||||
const returnTo = safeReturnTo(search.get('returnTo')) ?? '/scenarios';
|
||||
const expired = search.get('expired') === '1';
|
||||
const signedOut = search.get('notice') === 'signedOut';
|
||||
usePageTitle(t('auth.page_title'));
|
||||
|
||||
const handleSubmit = useCallback(
|
||||
async (username: string, password: string) => {
|
||||
await auth.login(username, password);
|
||||
navigate(returnTo, { replace: true });
|
||||
},
|
||||
[auth, navigate, returnTo],
|
||||
);
|
||||
|
||||
return (
|
||||
<div className={styles.page}>
|
||||
<header className={styles.header}>
|
||||
<img src={liqaLogo} alt="Liqa" className={styles.logo} />
|
||||
<ThemeSwitcher />
|
||||
</header>
|
||||
<main className={styles.main}>
|
||||
<LoginForm expired={expired} signedOut={signedOut} onSubmit={handleSubmit} />
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
.root {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--space-2);
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.button {
|
||||
min-height: 44px;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: var(--space-2);
|
||||
padding: var(--space-2) var(--space-3);
|
||||
color: var(--color-text);
|
||||
background: transparent;
|
||||
border: var(--border-width) solid var(--color-border);
|
||||
border-radius: var(--radius-md);
|
||||
font: inherit;
|
||||
font-size: var(--font-size-xs);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.button:hover:not(:disabled) {
|
||||
background: var(--color-secondary);
|
||||
}
|
||||
|
||||
.button:focus-visible,
|
||||
.failure button:focus-visible {
|
||||
outline: 2px solid var(--color-primary-text);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
.button:disabled {
|
||||
opacity: 0.55;
|
||||
cursor: wait;
|
||||
}
|
||||
|
||||
.failure {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--space-2);
|
||||
padding: var(--space-2);
|
||||
color: var(--color-error-fg);
|
||||
background: var(--color-error-bg);
|
||||
border-radius: var(--radius-sm);
|
||||
font-size: var(--font-size-xs);
|
||||
}
|
||||
|
||||
.failure button {
|
||||
min-height: 32px;
|
||||
color: inherit;
|
||||
background: transparent;
|
||||
border: var(--border-width) solid currentColor;
|
||||
border-radius: var(--radius-sm);
|
||||
cursor: pointer;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { LogOut } from 'lucide-react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import styles from './SignOutControl.module.css';
|
||||
|
||||
export function SignOutControl({
|
||||
pending = false,
|
||||
failed = false,
|
||||
onSignOut,
|
||||
}: {
|
||||
pending?: boolean;
|
||||
failed?: boolean;
|
||||
onSignOut: () => void;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
return (
|
||||
<div className={styles.root}>
|
||||
<button
|
||||
type="button"
|
||||
className={styles.button}
|
||||
onClick={onSignOut}
|
||||
disabled={pending}
|
||||
data-sign-out="true"
|
||||
aria-label={t(pending ? 'auth.signing_out' : 'auth.sign_out')}
|
||||
title={t(pending ? 'auth.signing_out' : 'auth.sign_out')}
|
||||
>
|
||||
<LogOut size={16} aria-hidden="true" />
|
||||
<span>{t(pending ? 'auth.signing_out' : 'auth.sign_out')}</span>
|
||||
</button>
|
||||
{failed && (
|
||||
<div className={styles.failure} role="alert">
|
||||
<span>{t('auth.sign_out_failed')}</span>
|
||||
<button type="button" onClick={onSignOut} disabled={pending}>
|
||||
{t('auth.retry')}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user