import React, { useRef, useState } from "react"; import ReCAPTCHA from "react-google-recaptcha"; import { useNavigate } from "react-router-dom"; const captchaKey = import.meta.env.VITE_CAPTCHA_KEY; const captchaSecret = import.meta.env.VITE_CAPTCHA_SECRET; const serverlUrl = import.meta.env.VITE_SERVER_URL; export default function SignUpForm({ registerUser }) { const [formValue, setFormValue] = useState({ email: "", fullname: "", password: "", repeatPassword: "", }); const [errorValue, setErrorValue] = useState({}); const [userExists, setUserExists] = useState(undefined); const [loading, setLoading] = useState(false); const [captchaError, setCaptchaError] = useState(false); const navigate = useNavigate(); const captchaRef = useRef(); const submitForm = async (e) => { e.preventDefault(); setLoading(true); setErrorValue(validateForm(formValue)); if (Object.keys(validateForm(formValue)).length > 0) { setLoading(false); return null; } else { let captchaToken = captchaRef.current?.getValue(); if (captchaToken.length === 0) { setCaptchaError("ReCaptcha is mandatory"); setLoading(false); return; } window.scrollTo(0, 0); const verify = await verifyCaptcha(captchaToken); captchaRef.current?.reset(); if (verify) { const registerationSuccess = await registerUser(formValue); setFormValue({ email: "", fullname: "", password: "", repeatPassword: "", }); if (registerationSuccess) { setLoading(false); setUserExists(false); navigate("/sign-in"); window.scrollTo(0, 0); } else { setLoading(false); setUserExists(true); } } else { setFormValue({ firstname: "", lastname: "", email: "", message: "", }); setCaptchaError(""); setLoading(false); } } }; const handleForm = (e) => { const { name, value } = e.target; setFormValue({ ...formValue, [name]: value }); }; const verifyCaptcha = async (captchaToken) => { try { const response = await fetch(serverlUrl, { method: "POST", body: JSON.stringify({ secret: captchaSecret, captchaToken, }), headers: { "Content-Type": "application/json", }, }); if (response.status === 200) { return true; } else { return false; } } catch (error) { console.error("Could not verify captcha!", error.message); return false; } }; const validateForm = (value) => { const errors = {}; const emailRegex = /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/; const numberRegex = /\d/; if (!value.email) { errors.email = "Enter the email"; } else if (!emailRegex.test(value.email)) { errors.email = "The email is not valid"; } if (!value.password) { errors.password = "Password field is required"; } if (!value.repeatPassword) { errors.repeatPassword = "Password field is required"; } if (value.password.length < 8 && value.password) { errors.password = "Password must be min. 8 characters"; } if (value.repeatPassword < 8 && value.repeatPassword) { errors.repeatPassword = "Password must be min. 8 characters"; } else if ( value.password && value.repeatPassword && value.password !== value.repeatPassword ) { errors.password = "Passwords don't match"; errors.repeatPassword = "Passwords don't match"; } if (!value.fullname) { errors.fullname = "Enter your name"; } else if (value.fullname.length < 4 && value.fullname) { errors.fullname = "Enter a valid name"; } else if (numberRegex.test(value.fullname)) { errors.fullname = "Please enter a valid name"; } return errors; }; return ( {loading ? (

Almost there...

) : (
{userExists !== undefined && userExists ? ( A user with such email already exists ) : null}
{errorValue.fullname}
{errorValue.email}
{errorValue.password}
{errorValue.repeatPassword}
{captchaError} )}
); }