2024-05-20 04:13:44 +04:00
|
|
|
|
'use client'
|
|
|
|
|
|
|
|
|
|
|
|
import {useRouter} from 'next/navigation'
|
|
|
|
|
|
import {useState} from 'react'
|
|
|
|
|
|
import AuthForm from '@/components/auth-form/index.js'
|
|
|
|
|
|
import AuthAlert from '@/components/auth-form/auth-alert.js'
|
|
|
|
|
|
|
|
|
|
|
|
export default function RegistrationForm() {
|
|
|
|
|
|
const router = useRouter()
|
|
|
|
|
|
const [error, setError] = useState('')
|
|
|
|
|
|
const [isOpen, setIsOpen] = useState(false)
|
|
|
|
|
|
|
|
|
|
|
|
const handleFormSubmit = async data => {
|
2024-07-02 11:07:01 +02:00
|
|
|
|
if (data.password_verification && data.password_verification !== data.password) {
|
|
|
|
|
|
setError('Les mots de passe ne correspondent pas !')
|
|
|
|
|
|
setIsOpen(true)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-20 04:13:44 +04:00
|
|
|
|
const response = await fetch('/api/auth/register', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
|
...data
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
if (response.status === 201) {
|
|
|
|
|
|
router.push('/login')
|
|
|
|
|
|
router.refresh()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const errorResponse = await response.json()
|
|
|
|
|
|
setError(errorResponse.message)
|
|
|
|
|
|
setIsOpen(true)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<>
|
|
|
|
|
|
{error && <AuthAlert isOpen={isOpen} setIsOpen={setIsOpen} message={error} severity='error' />}
|
|
|
|
|
|
<AuthForm
|
|
|
|
|
|
isRegister
|
|
|
|
|
|
title='Inscrivez-vous et devenez constituant'
|
|
|
|
|
|
buttonText='S’enregistrer'
|
|
|
|
|
|
linkDescription='Vous avez déjà un compte ?'
|
|
|
|
|
|
linkText='Se connecter'
|
|
|
|
|
|
linkHref='/login'
|
|
|
|
|
|
onSubmit={handleFormSubmit}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</>
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|