diff --git a/src/app/login/forgot-password.tsx b/src/app/login/forgot-password.tsx index 93180a2..cd2e685 100644 --- a/src/app/login/forgot-password.tsx +++ b/src/app/login/forgot-password.tsx @@ -18,6 +18,7 @@ import BaseTextInput from "@/components/forms/BaseTextInput"; import PasswordInput from "@/components/forms/PasswordInput"; import PrimaryButton from "@/components/buttons/PrimaryButton"; import FieldLabel from "@/components/forms/FieldLabel"; +import { validateEmail } from "@/constants/countries"; import { forgotPassword, resetPassword } from "@/features/auth/api/auth.api"; export default function ForgotPasswordScreen() { @@ -36,12 +37,9 @@ export default function ForgotPasswordScreen() { const handleRequestOtp = async () => { setEmailError(""); const cleanEmail = email.trim().toLowerCase(); - if (!cleanEmail) { - setEmailError("Email is required"); - return; - } - if (!/\S+@\S+\.\S+/.test(cleanEmail)) { - setEmailError("Please enter a valid email address"); + const emailErr = validateEmail(cleanEmail); + if (emailErr) { + setEmailError(emailErr); return; } diff --git a/src/constants/countries.ts b/src/constants/countries.ts index 9a6e2f9..7b227e6 100644 --- a/src/constants/countries.ts +++ b/src/constants/countries.ts @@ -256,3 +256,24 @@ export const updatePhoneDialCode = ( const bounded = nationalDigits.slice(0, newRule.maxLength); return bounded ? `${newRule.code} ${bounded}` : `${newRule.code} `; }; + +/** + * RFC 5322 & RFC 1035 compliant email validation regex. + * Disallows invalid domain characters (e.g. + in domain like +gmail+.com, consecutive dots, leading/trailing hyphens) + * while properly accepting plus-addressing in local-part (e.g. sanskar+sarak@gmail.com). + */ +export const EMAIL_REGEX = /^[a-zA-Z0-9_%+-]+(?:\.[a-zA-Z0-9_%+-]+)*@[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?)*\.[a-zA-Z]{2,}$/; + +export const validateEmail = (email?: string | null): string | null => { + if (!email || !email.trim()) { + return "Email is required"; + } + if (!EMAIL_REGEX.test(email.trim())) { + return "Please enter a valid email address"; + } + return null; +}; + +export const isValidEmail = (email?: string | null): boolean => { + return validateEmail(email) === null; +}; diff --git a/src/features/auth/components/LoginForm.tsx b/src/features/auth/components/LoginForm.tsx index 1ba7308..9ba3b51 100644 --- a/src/features/auth/components/LoginForm.tsx +++ b/src/features/auth/components/LoginForm.tsx @@ -17,6 +17,7 @@ import PasswordInput from "@/components/forms/PasswordInput"; import PrimaryButton from "@/components/buttons/PrimaryButton"; import { AuthColors } from "../constants/colors"; +import { validateEmail } from "@/constants/countries"; import { useAuthStore } from "@/store/auth.store"; import { LoginMethod } from "./LoginTabs"; import { login } from "@/features/auth/api/auth.api"; @@ -52,11 +53,9 @@ export default function LoginForm({ setPasswordError(""); if (activeTab === "email") { - if (!email.trim()) { - setEmailError("Email is required"); - hasError = true; - } else if (!/\S+@\S+\.\S+/.test(email)) { - setEmailError("Please enter a valid email address"); + const emailErr = validateEmail(email); + if (emailErr) { + setEmailError(emailErr); hasError = true; } } else { diff --git a/src/features/auth/components/SignUpForm.tsx b/src/features/auth/components/SignUpForm.tsx index ff12ded..78321d0 100644 --- a/src/features/auth/components/SignUpForm.tsx +++ b/src/features/auth/components/SignUpForm.tsx @@ -28,6 +28,7 @@ import { CountryCode, DEFAULT_COUNTRY, validatePhoneNumber, + validateEmail, } from "@/constants/countries"; import { useAuthStore } from "@/store/auth.store"; import { register } from "@/features/auth/api/auth.api"; @@ -92,11 +93,9 @@ export default function SignUpForm() { hasError = true; } - if (!email.trim()) { - setEmailError("Email is required"); - hasError = true; - } else if (!/\S+@\S+\.\S+/.test(email)) { - setEmailError("Please enter a valid email address"); + const emailErr = validateEmail(email); + if (emailErr) { + setEmailError(emailErr); hasError = true; }