import {useEffect, useState, forwardRef} from 'react' import {signIn} from 'next-auth/react' import {useRouter} from 'next/navigation' import PropTypes from 'prop-types' import Box from '@mui/material/Box' import Button from '@mui/material/Button' import Container from '@mui/material/Container' import FormControl from '@mui/material/FormControl' import IconButton from '@mui/material/IconButton' import Input from '@mui/material/Input' import InputAdornment from '@mui/material/InputAdornment' import InputLabel from '@mui/material/InputLabel' import LinearProgress from '@mui/material/LinearProgress' import Snackbar from '@mui/material/Snackbar' import Tab from '@mui/material/Tab' import Tabs from '@mui/material/Tabs' import Typography from '@mui/material/Typography' import Grid from '@mui/material/Grid' import Visibility from '@mui/icons-material/Visibility' import VisibilityOff from '@mui/icons-material/VisibilityOff' import MuiAlert from '@mui/material/Alert' import LoginIcon from '@mui/icons-material/Login' import AppRegistrationRoundedIcon from '@mui/icons-material/AppRegistrationRounded' import axios from 'axios' import {validateEmail} from '../../lib/utils/emails' import ResetPassword from '../password/reset-password' import ResetDialog from '../password/reset-dialog' import LoginProvider from './login-provider' const siteUrl = process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3001' const apiUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:1337' const PROVIDERS = [ { id: 'google', title: 'Google', width: 16, height: 16 }, { id: 'twitter', title: 'Twitter', width: 16, height: 13 } ] function TabPanel(props) { const {children, value, index, ...other} = props return ( ) } TabPanel.propTypes = { children: PropTypes.node, index: PropTypes.number.isRequired, value: PropTypes.number.isRequired, } function a11yProps(index) { return { id: `simple-tab-${index}`, 'aria-controls': `simple-tabpanel-${index}`, } } const Alert = forwardRef(function Alert(props, ref) { return }) function Koneksyon({chimen}) { const [error, setError] = useState('') const [loginCredentials, setLoginCredentials] = useState({username: '', password: ''}) const [registerCredentials, setRegisterCredentials] = useState({username: '', email: '', password: ''}) const [passwordVerification, setPasswordVerification] = useState('') const [showPassword, setShowPassword] = useState(false) const [showRegisterPassword, setShowRegisterPassword] = useState(false) const [showVerificationPassword, setShowVerificationPassword] = useState(false) const [loading, setLoading] = useState(false) const [open, setOpen] = useState(true) const [success, setSuccess] = useState(false) const [openDialog, setOpenDialog] = useState(false) const [value, setValue] = useState(0) const router = useRouter() const handleUpdate = update => { setLoginCredentials({...loginCredentials, ...update}) } const handleRegisterUpdate = update => { setRegisterCredentials({...registerCredentials, ...update}) } const handleChange = (event, newValue) => { setValue(newValue) } const register = async () => { try { const response = await axios.post(`${apiUrl}/auth/local/register`, { ...registerCredentials }, { headers: { 'content-type': 'application/json' } }) localStorage.setItem('username', `${response?.data?.user?.username}`) setSuccess(true) resetRegisterForm() } catch (error_) { if (error_.message.endsWith(400)) { setError('E-mail ou utilisateur déjà enregistré') } else { setError('Une erreur s’est produite') } } } const resetRegisterForm = () => { setRegisterCredentials({username: '', email: '', password: ''}) setPasswordVerification('') } const handleClickLogin = async () => { if (!validateEmail(loginCredentials.username) || loginCredentials.password === '') { return } setLoading(true) const response = await signIn('credentials', { callbackUrl: `${siteUrl}${chimen}`, redirect: false, ...loginCredentials }) if (response.error) { setError(response.error) setLoading(false) } else if (response.ok) { setLoading(false) router.push(chimen) } } const handleClickRegister = async () => { setLoading(true) if (!validateEmail(registerCredentials.email) || registerCredentials.password === '') { return } if (registerCredentials.password !== passwordVerification) { setError('Les 2 mots de passe de correspondent pas') setLoading(false) return } if (registerCredentials.username.length < 3) { setError('Le nom d’utilisateur est trop court, 3 caratères minimum') setLoading(false) return } if (registerCredentials.password.length < 6) { setError('Le mot de passe est trop court, 6 caratères minimum') setLoading(false) return } await register() setLoading(false) } const handleClose = (event, reason) => { if (reason === 'clickaway') { return } setOpen(false) setSuccess(false) setError('') } useEffect(() => { if (error) { setOpen(true) } return () => { setLoading(false) } }, [error]) const handleClickShowPassword = () => { setShowPassword(!showPassword) } const handleClickShowRegisterPassword = () => { setShowRegisterPassword(!showRegisterPassword) } const handleClickShowVerificationPassword = () => { setShowVerificationPassword(!showVerificationPassword) } const handleMouseDownPassword = event => { event.preventDefault() } const handleMouseDownRegisterPassword = event => { event.preventDefault() } const handleMouseDownVerificationPassword = event => { event.preventDefault() } const handleKeyUpLogin = event => { if (event.keyCode === 13) { handleClickLogin() } } const handleKeyUpRegister = event => { if (event.keyCode === 13) { handleClickRegister() } } return ( } label='Se connecter' {...a11yProps(0)} /> } label='S’inscrire' {...a11yProps(1)} /> Connectez-vous avec {PROVIDERS.map(({id, title, width, height}) => ( ))} ou utilisez votre e-mail pour vous identifier E-mail handleUpdate({username: event.target.value})} onKeyUp={handleKeyUpLogin} /> Mot de passe {showPassword ? : } } onChange={event => handleUpdate({password: event.target.value})} onKeyUp={handleKeyUpLogin} /> E-mail handleRegisterUpdate({email: event.target.value})} onKeyUp={handleKeyUpRegister} /> Nom d’utilisateur handleRegisterUpdate({username: event.target.value})} onKeyUp={handleKeyUpRegister} /> Mot de passe {showRegisterPassword ? : } } onChange={event => handleRegisterUpdate({password: event.target.value})} onKeyUp={handleKeyUpRegister} /> Vérification du mot de passe {showVerificationPassword ? : } } onChange={event => setPasswordVerification(event.target.value)} onKeyUp={handleKeyUpRegister} /> {loading && } {error && ( {error} )} {success && ( Veuillez confirmer votre adresse e-mail via le lien qui vous a été envoyé. )} ) } Koneksyon.propTypes = { chimen: PropTypes.string.isRequired } export default Koneksyon