- Add session-aware login, logout and protected navigation. - Send cookie credentials for API and run-event requests with localized UI stories.
124 lines
3.9 KiB
TypeScript
124 lines
3.9 KiB
TypeScript
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>
|
|
);
|
|
}
|