From 0306d670869a6998b650b032d71912fdde1d3a88 Mon Sep 17 00:00:00 2001 From: KGFCH2 Date: Sun, 31 May 2026 21:20:33 +0530 Subject: [PATCH] fix: enforce robust client-side email format and password length validation on login --- frontend/src/pages/LoginPage.jsx | 42 +++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/frontend/src/pages/LoginPage.jsx b/frontend/src/pages/LoginPage.jsx index 5ca7599..5e8c65b 100644 --- a/frontend/src/pages/LoginPage.jsx +++ b/frontend/src/pages/LoginPage.jsx @@ -10,11 +10,19 @@ export default function LoginPage() { const [password, setPassword] = useState(''); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); + const [isEmailValid, setIsEmailValid] = useState(true); const auth = useAuth(); const navigate = useNavigate(); const [searchParams] = useSearchParams(); + const isPasswordValid = password.length >= 6; + + const validateEmail = (value) => { + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + return emailRegex.test(value); + }; + // Handle GitHub OAuth callback redirect: // Backend sends: /login?authStatus=success#token=JWT useEffect(() => { @@ -47,6 +55,17 @@ export default function LoginPage() { const handleSubmit = async (e) => { e.preventDefault(); setError(''); + + if (!validateEmail(email)) { + setError('Please enter a valid email address'); + return; + } + + if (!isPasswordValid) { + setError('Password must be at least 6 characters'); + return; + } + setLoading(true); try { const response = await authService.login(email, password); @@ -73,8 +92,8 @@ export default function LoginPage() { {error && ( -
-

+

+

{error}

@@ -100,7 +119,7 @@ export default function LoginPage() {
-
+
@@ -123,10 +151,16 @@ export default function LoginPage() { type="password" value={password} onChange={(e) => setPassword(e.target.value)} + aria-invalid={password.length > 0 && !isPasswordValid} className="w-full p-5 border-4 border-black rounded-none text-black font-bold focus:outline-none focus:border-gray-500" placeholder="••••••••" required /> + {password && !isPasswordValid && ( +

+ Password must be at least 6 characters +

+ )}