fix(auth): standardize RFC email validation across mobile auth screens
This commit is contained in:
parent
bf167e3f0a
commit
829fa5b5cb
|
|
@ -18,6 +18,7 @@ import BaseTextInput from "@/components/forms/BaseTextInput";
|
||||||
import PasswordInput from "@/components/forms/PasswordInput";
|
import PasswordInput from "@/components/forms/PasswordInput";
|
||||||
import PrimaryButton from "@/components/buttons/PrimaryButton";
|
import PrimaryButton from "@/components/buttons/PrimaryButton";
|
||||||
import FieldLabel from "@/components/forms/FieldLabel";
|
import FieldLabel from "@/components/forms/FieldLabel";
|
||||||
|
import { validateEmail } from "@/constants/countries";
|
||||||
import { forgotPassword, resetPassword } from "@/features/auth/api/auth.api";
|
import { forgotPassword, resetPassword } from "@/features/auth/api/auth.api";
|
||||||
|
|
||||||
export default function ForgotPasswordScreen() {
|
export default function ForgotPasswordScreen() {
|
||||||
|
|
@ -36,12 +37,9 @@ export default function ForgotPasswordScreen() {
|
||||||
const handleRequestOtp = async () => {
|
const handleRequestOtp = async () => {
|
||||||
setEmailError("");
|
setEmailError("");
|
||||||
const cleanEmail = email.trim().toLowerCase();
|
const cleanEmail = email.trim().toLowerCase();
|
||||||
if (!cleanEmail) {
|
const emailErr = validateEmail(cleanEmail);
|
||||||
setEmailError("Email is required");
|
if (emailErr) {
|
||||||
return;
|
setEmailError(emailErr);
|
||||||
}
|
|
||||||
if (!/\S+@\S+\.\S+/.test(cleanEmail)) {
|
|
||||||
setEmailError("Please enter a valid email address");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -256,3 +256,24 @@ export const updatePhoneDialCode = (
|
||||||
const bounded = nationalDigits.slice(0, newRule.maxLength);
|
const bounded = nationalDigits.slice(0, newRule.maxLength);
|
||||||
return bounded ? `${newRule.code} ${bounded}` : `${newRule.code} `;
|
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 PrimaryButton from "@/components/buttons/PrimaryButton";
|
||||||
|
|
||||||
import { AuthColors } from "../constants/colors";
|
import { AuthColors } from "../constants/colors";
|
||||||
|
import { validateEmail } from "@/constants/countries";
|
||||||
import { useAuthStore } from "@/store/auth.store";
|
import { useAuthStore } from "@/store/auth.store";
|
||||||
import { LoginMethod } from "./LoginTabs";
|
import { LoginMethod } from "./LoginTabs";
|
||||||
import { login } from "@/features/auth/api/auth.api";
|
import { login } from "@/features/auth/api/auth.api";
|
||||||
|
|
@ -52,11 +53,9 @@ export default function LoginForm({
|
||||||
setPasswordError("");
|
setPasswordError("");
|
||||||
|
|
||||||
if (activeTab === "email") {
|
if (activeTab === "email") {
|
||||||
if (!email.trim()) {
|
const emailErr = validateEmail(email);
|
||||||
setEmailError("Email is required");
|
if (emailErr) {
|
||||||
hasError = true;
|
setEmailError(emailErr);
|
||||||
} else if (!/\S+@\S+\.\S+/.test(email)) {
|
|
||||||
setEmailError("Please enter a valid email address");
|
|
||||||
hasError = true;
|
hasError = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ import {
|
||||||
CountryCode,
|
CountryCode,
|
||||||
DEFAULT_COUNTRY,
|
DEFAULT_COUNTRY,
|
||||||
validatePhoneNumber,
|
validatePhoneNumber,
|
||||||
|
validateEmail,
|
||||||
} from "@/constants/countries";
|
} from "@/constants/countries";
|
||||||
import { useAuthStore } from "@/store/auth.store";
|
import { useAuthStore } from "@/store/auth.store";
|
||||||
import { register } from "@/features/auth/api/auth.api";
|
import { register } from "@/features/auth/api/auth.api";
|
||||||
|
|
@ -92,11 +93,9 @@ export default function SignUpForm() {
|
||||||
hasError = true;
|
hasError = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!email.trim()) {
|
const emailErr = validateEmail(email);
|
||||||
setEmailError("Email is required");
|
if (emailErr) {
|
||||||
hasError = true;
|
setEmailError(emailErr);
|
||||||
} else if (!/\S+@\S+\.\S+/.test(email)) {
|
|
||||||
setEmailError("Please enter a valid email address");
|
|
||||||
hasError = true;
|
hasError = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue