'use client' import React, { useState } from 'react' import { useRouter } from 'next/navigation' interface RegisterFormProps { messages: { common: { name: string email: string password: string confirmPassword: string } register: { namePlaceholder: string emailPlaceholder: string passwordPlaceholder: string confirmPasswordPlaceholder: string button: string loading: string error: string passwordMismatch: string } } } export default function RegisterForm({ messages }: RegisterFormProps) { const router = useRouter() const [error, setError] = useState(null) const [loading, setLoading] = useState(false) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) setError(null) const formData = new FormData(e.currentTarget) const name = formData.get('name') as string const email = formData.get('email') as string const password = formData.get('password') as string const confirmPassword = formData.get('confirmPassword') as string if (password !== confirmPassword) { setError(messages.register.passwordMismatch) setLoading(false) return } try { const response = await fetch('/api/auth/register', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ name, email, password, }), }) const data = await response.json() if (!response.ok) { throw new Error(data.message || messages.register.error) } router.push('/auth/login?registered=true') } catch (error) { setError(error instanceof Error ? error.message : messages.register.error) } finally { setLoading(false) } } return (
{error && (
{error}
)}
) }