fix(auth): standardize RFC email validation across mobile auth screens #112
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue