'use client'; import { useState } from 'react'; import { useRouter, useSearchParams } from 'next/navigation'; import Link from 'next/link'; import { Field } from '@/components/ui'; import { t } from '@/lib/i18n'; export function AuthForm({ mode }: { mode: 'login' | 'register' }) { const router = useRouter(); const params = useSearchParams(); const next = params.get('next') || '/dashboard'; const [busy, setBusy] = useState(false); const [error, setError] = useState(null); async function onSubmit(e: React.FormEvent) { e.preventDefault(); setBusy(true); setError(null); const form = new FormData(e.currentTarget); const body = Object.fromEntries(form.entries()); try { const res = await fetch(`/api/auth/${mode}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }); if (!res.ok) { const data = (await res.json().catch(() => ({}))) as { error?: string }; throw new Error(data.error || `Request failed (${res.status})`); } router.push(next); router.refresh(); } catch (err) { setError(err instanceof Error ? err.message : 'Request failed'); setBusy(false); } } const isRegister = mode === 'register'; return (

{isRegister ? t('auth.registerTitle') : t('auth.loginTitle')}

{isRegister && ( )} {error &&

{error}

}

{isRegister ? t('auth.hasAccount') : t('auth.noAccount')}{' '} {isRegister ? t('auth.submitLogin') : t('auth.submitRegister')}

{!isRegister &&

{t('auth.demoHint')}

}
); }