import { useState, type FormEvent } from 'react' import { Navigate, useLocation, type Location } from 'react-router' import { errorMessage } from '../api' import { card, inputClass, labelText, primaryButton } from '../ui' import { useAuth } from './AuthContext' export function LoginForm() { const { user, login, register } = useAuth() const location = useLocation() const [mode, setMode] = useState<'login' | 'register'>('login') const [username, setUsername] = useState('') const [password, setPassword] = useState('') const [error, setError] = useState(null) const [busy, setBusy] = useState(false) if (user) { const from = (location.state as { from?: Location } | null)?.from return } const isLogin = mode === 'login' async function submit(event: FormEvent) { event.preventDefault() setError(null) setBusy(true) try { await (isLogin ? login : register)(username, password) } catch (err) { setError(errorMessage(err)) } finally { setBusy(false) } } return (

{isLogin ? 'Sign in' : 'Create account'}

{error && (

{error}

)}
) }