2024-05-20 04:14:46 +04:00
|
|
|
|
'use client'
|
|
|
|
|
|
|
|
|
|
|
|
import {signIn} from 'next-auth/react'
|
|
|
|
|
|
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 LoginForm() {
|
|
|
|
|
|
const router = useRouter()
|
|
|
|
|
|
const [error, setError] = useState('')
|
|
|
|
|
|
const [isOpen, setIsOpen] = useState(false)
|
|
|
|
|
|
|
|
|
|
|
|
const handleFormSubmit = async data => {
|
|
|
|
|
|
const response = await signIn('credentials', {
|
|
|
|
|
|
email: data.email,
|
|
|
|
|
|
password: data.password,
|
|
|
|
|
|
redirect: false
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
if (response?.error) {
|
2024-07-02 11:06:15 +02:00
|
|
|
|
if (response.error === 'Configuration') {
|
|
|
|
|
|
setError('Une erreur s’est produite, contactez l’administrateur !')
|
|
|
|
|
|
} else {
|
|
|
|
|
|
setError(response.error)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-20 04:14:46 +04:00
|
|
|
|
setIsOpen(true)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
router.push('/')
|
|
|
|
|
|
router.refresh()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<>
|
|
|
|
|
|
{error && <AuthAlert isOpen={isOpen} setIsOpen={setIsOpen} message={error} severity='error' />}
|
|
|
|
|
|
<AuthForm
|
|
|
|
|
|
title='Connectez-vous à votre compte'
|
|
|
|
|
|
buttonText='Se connecter'
|
|
|
|
|
|
linkDescription='Vous n’avez pas de compte ?'
|
|
|
|
|
|
linkText='S’inscrire'
|
|
|
|
|
|
linkHref='/register'
|
|
|
|
|
|
isRegister={false}
|
|
|
|
|
|
onSubmit={handleFormSubmit}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</>
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|