File size: 10,129 Bytes
f5071ca |
1 2 3 4 5 6 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
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 (
<React.Fragment>
{loading ? (
<div role="status" className="flex flex-col gap-4 items-center">
<p className="text-indigo-400 text-xl">Almost there...</p>
<svg
aria-hidden="true"
className="w-10 h-10 mt-6 text-gray-200 animate-spin dark:text-gray-600 fill-indigo-400"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg">
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
</div>
) : (
<form onSubmit={submitForm} className="mt-8 space-y-6">
{userExists !== undefined && userExists ? (
<span className="text-red-400">
A user with such email already exists
</span>
) : null}
<section>
<label htmlFor="fullname" className="sr-only">
Full name
</label>
<input
onChange={handleForm}
value={formValue.fullname}
id="fullname"
name="fullname"
type="text"
autoComplete="full-name"
className="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-white rounded-t-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
placeholder="Full name"
/>
<span className="text-red-400">{errorValue.fullname}</span>
</section>
<section>
<label htmlFor="email" className="sr-only">
Email address
</label>
<input
onChange={handleForm}
value={formValue.email}
id="email"
name="email"
type="email"
autoComplete="email"
className="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-white rounded-t-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
placeholder="Email address"
/>
<span className="text-red-400">{errorValue.email}</span>
</section>
<section>
<label htmlFor="password" className="sr-only">
Password
</label>
<input
onChange={handleForm}
value={formValue.password}
id="password"
name="password"
type="password"
autoComplete="current-password"
className="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-white rounded-t-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
placeholder="Password"
/>
<span className="text-red-400">{errorValue.password}</span>
</section>
<section>
<label htmlFor="repeatPassword" className="sr-only">
Reapeat password
</label>
<input
onChange={handleForm}
value={formValue.repeatPassword}
id="repeatPassword"
name="repeatPassword"
type="password"
autoComplete="current-password"
className="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-white rounded-t-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
placeholder="Reapeat password"
/>
<span className="text-red-400">{errorValue.repeatPassword}</span>
</section>
<ReCAPTCHA ref={captchaRef} sitekey={captchaKey} />
<span className="text-red-400">{captchaError}</span>
<button
type="submit"
className="group relative w-full flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 cursor-pointer">
<span className="absolute left-0 inset-y-0 flex items-center pl-3">
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="currentColor"
className="h-5 w-5 text-[color:var(--primary-font-color)] group-hover:text-[color:var(--primary-font-color-hover)]"
aria-hidden="true">
<path
fillRule="evenodd"
d="M12 1.5a5.25 5.25 0 0 0-5.25 5.25v3a3 3 0 0 0-3 3v6.75a3 3 0 0 0 3 3h10.5a3 3 0 0 0 3-3v-6.75a3 3 0 0 0-3-3v-3c0-2.9-2.35-5.25-5.25-5.25Zm3.75 8.25v-3a3.75 3.75 0 1 0-7.5 0v3h7.5Z"
clipRule="evenodd"
/>
</svg>
</span>
Sign up
</button>
</form>
)}
</React.Fragment>
);
}
|