diff --git a/src/components/login/LoginSignup.css b/src/components/login/LoginSignup.css index 0f92cae..117834e 100644 --- a/src/components/login/LoginSignup.css +++ b/src/components/login/LoginSignup.css @@ -154,9 +154,10 @@ .error-message { color: red; - font-size: 12px; + font-size: 14px; margin-top: 5px; + text-align: start; } -} + diff --git a/src/components/login/LoginSignup.jsx b/src/components/login/LoginSignup.jsx index 6daadd6..ab529b6 100644 --- a/src/components/login/LoginSignup.jsx +++ b/src/components/login/LoginSignup.jsx @@ -2,25 +2,60 @@ import React, { useState } from "react"; import { Link } from "react-router-dom"; // Import Link import { FaArrowLeft } from "react-icons/fa"; // Import Font Awesome arrow left icon import "./LoginSignup.css"; -import {FaEye,FaEyeSlash} from 'react-icons/fa6' +import { FaEye, FaEyeSlash } from 'react-icons/fa6' const LoginSignup = () => { -const [state, setState] = useState("Sign Up"); -const [isVisible, setIsVisible] = useState(false); + const [state, setState] = useState("Sign Up"); + const [isVisible, setIsVisible] = useState(false); const [formData, setFormData] = useState({ username: "", password: "", email: "", }); + + const [errors, setErrors] = useState({ + username: "", + password: "", + email: "", + }); + const handleSignInWithGoogle = () => { window.location.href = 'http://localhost:4000/auth/google'; // Redirect to the server route for Google OAuth login }; const ChangeHandler = (e) => { setFormData({ ...formData, [e.target.name]: e.target.value }); + setErrors({ ...errors, [e.target.name]: "" }); + }; + + const validate = () => { + let isValid = true; + const newErrors = { + username: "", + password: "", + email: "", + }; + + if (state === "Sign Up" && !formData.username) { + newErrors.username = "Please enter your name."; + isValid = false; + } + if (!formData.email) { + newErrors.email = "Please enter your email."; + isValid = false; + } + if (!formData.password) { + newErrors.password = "Please enter your password."; + isValid = false; + } + + setErrors(newErrors); + return isValid; }; + const login = async () => { + if (!validate()) return; console.log("login"); let responseData; await fetch("http://localhost:4000/login", { @@ -40,6 +75,7 @@ const [isVisible, setIsVisible] = useState(false); } }; const signup = async () => { + if (!validate()) return; console.log("Sign up"); let responseData; await fetch("http://localhost:4000/signup", { @@ -49,71 +85,63 @@ const [isVisible, setIsVisible] = useState(false); 'content-Type': 'application/json' }, body: JSON.stringify(formData) - }).then((response) => response.json()).then((data) => responseData = data) + }).then((response) => response.json()).then((data) => responseData = data); if (responseData.success) { localStorage.setItem('auth-token', responseData.token); - window.location.replace("/") - } - else { + window.location.replace("/"); + } else { alert(responseData.errors); } }; - return (