diff --git a/Frontend/src/pages/Authentication/SignUp.jsx b/Frontend/src/pages/Authentication/SignUp.jsx index 77849e6..e431f08 100644 --- a/Frontend/src/pages/Authentication/SignUp.jsx +++ b/Frontend/src/pages/Authentication/SignUp.jsx @@ -1,5 +1,5 @@ import { useState } from "react"; -import { FiEye, FiEyeOff } from "react-icons/fi"; +import { FiEye, FiEyeOff, FiLoader } from "react-icons/fi"; import { Link, useNavigate } from "react-router-dom"; import toast, { Toaster } from "react-hot-toast"; import { useTranslation } from "react-i18next"; // for multilinguality @@ -7,12 +7,13 @@ import { useTranslation } from "react-i18next"; // for multilinguality const API_URL = import.meta.env.VITE_API_URL; const SignUp = () => { - const { t } = useTranslation(); // for multilinguality + const { t } = useTranslation(); const navigate = useNavigate(); const [formData, setFormData] = useState({ firstname: "", lastname: "", + username: "", email: "", password: "", confirmPassword: "", @@ -20,19 +21,30 @@ const SignUp = () => { const [showPassword, setShowPassword] = useState(false); const [showConfirmPassword, setShowConfirmPassword] = useState(false); const [loading, setLoading] = useState(false); + const [errors, setErrors] = useState({}); + + const validate = () => { + const errs = {}; + if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(formData.email)) + errs.email = t("invalid_email"); + if (formData.password.length < 8) errs.password = t("password_too_short"); + if (formData.password !== formData.confirmPassword) + errs.confirmPassword = t("passwords_do_not_match"); + if (formData.username.length < 3) errs.username = t("username_too_short"); + return errs; + }; const handleChange = (e) => { - setFormData((prev) => ({ - ...prev, - [e.target.name]: e.target.value, - })); + const { name, value } = e.target; + setFormData((prev) => ({ ...prev, [name]: value })); + setErrors((prev) => ({ ...prev, [name]: undefined })); }; const handleSubmit = async (e) => { e.preventDefault(); - - if (formData.password !== formData.confirmPassword) { - toast.error(t("passwords_do_not_match")); + const validation = validate(); + if (Object.keys(validation).length) { + setErrors(validation); return; } @@ -40,8 +52,7 @@ const SignUp = () => { const toastId = toast.loading(t("registering")); try { - // 1️⃣ Sign up the user - const signupRes = await fetch(`${API_URL}/api/signup`, { + const signupRes = await fetch(`${API_URL}/api/auth/register`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ @@ -49,6 +60,8 @@ const SignUp = () => { lastname: formData.lastname, email: formData.email, password: formData.password, + username: formData.username, + fullname: `${formData.firstname} ${formData.lastname}`, }), }); const signupData = await signupRes.json(); @@ -58,25 +71,18 @@ const SignUp = () => { return; } - // 2️⃣ Build username and create HDFS folder - const username = - `${formData.firstname}${formData.lastname}`.toLowerCase(); const folderRes = await fetch( - `${API_URL}/api/hdfs/createFolder?hdfsPath=/${username}`, + `${API_URL}/api/hdfs/createFolder?hdfsPath=/${formData.username}`, { method: "POST" } ); if (!folderRes.ok) { - // you might choose to roll back user creation or just notify toast.error(t("failed_create_folder"), { id: toastId }); } else { toast.success(t("signup_success"), { id: toastId }); } - // 3️⃣ Redirect to login after a short delay - setTimeout(() => { - navigate("/login"); - }, 1500); + setTimeout(() => navigate("/login"), 1500); } catch (error) { console.error(error); toast.error(t("an_error_occurred"), { id: toastId }); @@ -89,91 +95,147 @@ const SignUp = () => {
{t("already_have_account")}{" "}