Merge pull request 'fix(auth): enforce RFC-compliant email validation across login, registration and password recovery' (#89) from fix/email-format-validation into main
Reviewed-on: https://git.zedobject.local/AcuPedia/acupedia-store-ui/pulls/89
This commit is contained in:
commit
ccc3d9c250
26
src/App.tsx
26
src/App.tsx
|
|
@ -6,7 +6,7 @@ import { getCountryRuleByName } from './constants/countries';
|
|||
import type { ModuleItem, Currency, UserProfile, VerificationResult, PublicPlan } from './types';
|
||||
import { API_BASE } from './services/api';
|
||||
import { loadRazorpayScript } from './services/razorpay';
|
||||
import { cleanPhoneInput, validatePhoneNumber, validatePassword } from './utils/validation';
|
||||
import { cleanPhoneInput, validatePhoneNumber, validatePassword, validateEmail } from './utils/validation';
|
||||
|
||||
// Modular Subcomponents
|
||||
import { StoreHeader } from './components/layout/StoreHeader';
|
||||
|
|
@ -408,15 +408,9 @@ export default function App() {
|
|||
|
||||
const trimmedEmail = authEmail.trim();
|
||||
|
||||
if (!trimmedEmail) {
|
||||
setAuthError('Email address is required.');
|
||||
setAuthLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
if (!emailRegex.test(trimmedEmail)) {
|
||||
setAuthError('Please enter a valid email address.');
|
||||
const emailErr = validateEmail(trimmedEmail);
|
||||
if (emailErr) {
|
||||
setAuthError(emailErr);
|
||||
setAuthLoading(false);
|
||||
return;
|
||||
}
|
||||
|
|
@ -552,15 +546,9 @@ export default function App() {
|
|||
setResetSuccessMessage(null);
|
||||
|
||||
const trimmedEmail = authEmail.trim();
|
||||
if (!trimmedEmail) {
|
||||
setAuthError('Email address is required.');
|
||||
setAuthLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
if (!emailRegex.test(trimmedEmail)) {
|
||||
setAuthError('Please enter a valid email address.');
|
||||
const emailErr = validateEmail(trimmedEmail);
|
||||
if (emailErr) {
|
||||
setAuthError(emailErr);
|
||||
setAuthLoading(false);
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -179,3 +179,24 @@ export const validatePassword = (password: string, fieldName = 'Password'): stri
|
|||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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 address 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;
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue