Newer
Older
import React, { useState } from "react";
import Link from "next/link";
import { useRouter } from "next/navigation";
export default function Register() {
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const router = useRouter();
const [firstname, setFirstname] = useState('');
const [lastname, setLastname] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [dob, setDob] = useState('');
const [error, setError] = useState<string | null>(null);
const [isSubmitting, setIsSubmitting] = useState(false);
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
setError(null);
setIsSubmitting(true);
try {
const response = await fetch('/api/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
firstname,
lastname,
email,
password,
dob,
}),
});
const data = await response.json();
if (!response.ok) {
throw new Error(data.message || "Une erreur est survenue.");
}
router.push('/login');
} catch (err: any) {
setError(err.message);
} finally {
setIsSubmitting(false);
}
};
<div className="min-h-screen bg-gray-50 flex flex-col justify-center items-center p-4">
<div className="max-w-md w-full mx-auto">
<div className="text-center mb-8">
<Link href="/" className="text-3xl font-bold text-gray-900">
Immo<span className="text-gray-500">Next</span>
</Link>
<h2 className="mt-6 text-3xl font-extrabold text-gray-900">Créer un compte</h2>
<p className="mt-2 text-sm text-gray-600">
Déjà membre ?{' '}
<Link href="/login" className="font-medium text-gray-800 hover:text-black underline">
Connectez-vous
</Link>
</p>
</div>
<div className="bg-white p-8 border border-gray-200 rounded-xl shadow-sm">
<form onSubmit={handleSubmit} className="space-y-6">
<div>
<label htmlFor="firstname" className="block text-sm font-medium text-gray-700">Prénom</label>
<input id="firstname" type="text" required value={firstname} onChange={(e) => setFirstname(e.target.value)} className="mt-1 block w-full px-3 py-2 border border-gray-300 text-gray-700 rounded-md shadow-sm focus:outline-none focus:ring-gray-500 focus:border-gray-500" />
</div>
<div>
<label htmlFor="lastname" className="block text-sm font-medium text-gray-700">Nom</label>
<input id="lastname" type="text" required value={lastname} onChange={(e) => setLastname(e.target.value)} className="mt-1 block w-full px-3 py-2 border border-gray-300 text-gray-700 rounded-md shadow-sm focus:outline-none focus:ring-gray-500 focus:border-gray-500" />
<label htmlFor="email" className="block text-sm font-medium text-gray-700">Adresse e-mail</label>
<input id="email" type="email" required value={email} onChange={(e) => setEmail(e.target.value)} className="mt-1 block w-full px-3 py-2 border border-gray-300 text-gray-700 rounded-md shadow-sm focus:outline-none focus:ring-gray-500 focus:border-gray-500" />
</div>
<div>
<label htmlFor="password" className="block text-sm font-medium text-gray-700">Mot de passe</label>
<input id="password" type="password" required value={password} onChange={(e) => setPassword(e.target.value)} className="mt-1 block w-full px-3 py-2 border border-gray-300 text-gray-700 rounded-md shadow-sm focus:outline-none focus:ring-gray-500 focus:border-gray-500" />
</div>
<div>
<label htmlFor="dob" className="block text-sm font-medium text-gray-700">Date de naissance</label>
<input id="dob" type="date" required value={dob} onChange={(e) => setDob(e.target.value)} className="mt-1 block w-full px-3 py-2 border border-gray-300 text-gray-700 rounded-md shadow-sm focus:outline-none focus:ring-gray-500 focus:border-gray-500" />
</div>
{error && <p className="text-sm text-red-600">{error}</p>}
<div>
<button type="submit" disabled={isSubmitting} className="w-full flex justify-center py-3 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-gray-900 hover:bg-black focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-black disabled:bg-gray-400">
{isSubmitting ? 'Inscription en cours...' : "S'inscrire"}
</div>
);