'use client'; import { useState, FormEvent } from 'react'; import { signIn } from 'next-auth/react'; import { useRouter } from 'next/navigation'; const BACKEND_URL = process.env.NEXT_PUBLIC_API_URL ?? '/api'; export default function RegisterPage() { const router = useRouter(); const [form, setForm] = useState({ username: '', email: '', password: '', confirm: '' }); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); async function handleSubmit(e: FormEvent) { e.preventDefault(); setError(''); if (form.password !== form.confirm) { setError('Lösenorden matchar inte'); return; } setLoading(true); const res = await fetch('/api/auth-register', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: form.username, email: form.email, password: form.password }), }); if (!res.ok) { const data = await res.json().catch(() => ({})); setError(data.message ?? 'Registrering misslyckades'); setLoading(false); return; } // Auto-login after register await signIn('credentials', { username: form.username, password: form.password, redirect: false }); router.push('/'); router.refresh(); } return (

Skapa konto

{(['username', 'email', 'password', 'confirm'] as const).map((field) => (
setForm((f) => ({ ...f, [field]: e.target.value }))} required minLength={field.includes('password') || field === 'confirm' ? 8 : undefined} style={{ width: '100%', padding: '8px 12px', borderRadius: 6, border: '1px solid #ccc', fontSize: '1rem' }} />
))} {error &&

{error}

}

Har du redan ett konto? Logga in

); }