import { type FormEvent, type ReactNode, useState } from 'react'; import { useLocation, useNavigate } from 'react-router-dom'; import { useAuth } from './auth-context'; import { Button } from '../../components/ui/button'; import { Card } from '../../components/ui/card'; import { Input } from '../../components/ui/input'; import { ApiError } from '../../lib/api-client'; import { loginSchema } from '../../lib/schemas'; /** Zentrale Anmeldeseite der Plattform. */ export function LoginPage(): ReactNode { const { login } = useAuth(); const navigate = useNavigate(); const location = useLocation(); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(null); const [fieldErrors, setFieldErrors] = useState>({}); const [submitting, setSubmitting] = useState(false); const from = (location.state as { from?: string } | null)?.from ?? '/'; async function handleSubmit(event: FormEvent): Promise { event.preventDefault(); setError(null); setFieldErrors({}); const parsed = loginSchema.safeParse({ username, password }); if (!parsed.success) { const errors: Record = {}; for (const issue of parsed.error.issues) { const key = String(issue.path[0]); if (!errors[key]) errors[key] = issue.message; } setFieldErrors(errors); return; } setSubmitting(true); try { await login(parsed.data); navigate(from, { replace: true }); } catch (caughtError) { if (caughtError instanceof ApiError) { setError(caughtError.message); } else { setError('Anmeldung fehlgeschlagen. Bitte später erneut versuchen.'); } } finally { setSubmitting(false); } } return (
M

MPM

Modulare Management-Plattform

void handleSubmit(event)} className="" noValidate >
setUsername(event.target.value)} error={fieldErrors.username} autoFocus /> setPassword(event.target.value)} error={fieldErrors.password} />
{error && (

{error}

)}
); }