153 lines
5.0 KiB
TypeScript
153 lines
5.0 KiB
TypeScript
'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<string | null>(null)
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
|
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 (
|
|
<form className="mt-8 space-y-6" onSubmit={handleSubmit}>
|
|
<div className="rounded-md shadow-sm -space-y-px">
|
|
<div>
|
|
<label htmlFor="name" className="sr-only">
|
|
{messages.common.name}
|
|
</label>
|
|
<input
|
|
id="name"
|
|
name="name"
|
|
type="text"
|
|
required
|
|
className="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-t-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
|
|
placeholder={messages.register.namePlaceholder}
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label htmlFor="email" className="sr-only">
|
|
{messages.common.email}
|
|
</label>
|
|
<input
|
|
id="email"
|
|
name="email"
|
|
type="email"
|
|
autoComplete="email"
|
|
required
|
|
className="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
|
|
placeholder={messages.register.emailPlaceholder}
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label htmlFor="password" className="sr-only">
|
|
{messages.common.password}
|
|
</label>
|
|
<input
|
|
id="password"
|
|
name="password"
|
|
type="password"
|
|
autoComplete="new-password"
|
|
required
|
|
className="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
|
|
placeholder={messages.register.passwordPlaceholder}
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label htmlFor="confirmPassword" className="sr-only">
|
|
{messages.common.confirmPassword}
|
|
</label>
|
|
<input
|
|
id="confirmPassword"
|
|
name="confirmPassword"
|
|
type="password"
|
|
autoComplete="new-password"
|
|
required
|
|
className="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-b-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
|
|
placeholder={messages.register.confirmPasswordPlaceholder}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{error && (
|
|
<div className="rounded-md bg-red-50 p-4">
|
|
<div className="text-sm text-red-700">{error}</div>
|
|
</div>
|
|
)}
|
|
|
|
<div>
|
|
<button
|
|
type="submit"
|
|
disabled={loading}
|
|
className="group relative w-full flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
|
|
>
|
|
{loading ? messages.register.loading : messages.register.button}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
)
|
|
}
|