fix(auth): enforce country-wise phone number validation and 10-digit limit for India (#8)
- Enforce exact 10-digit mobile number requirement and input length restriction for India - Add country-wise phone length rules, placeholders, and helper hints in AuthModal - Synchronize dial code and country selection bidirectionally during registration - Validate phone numbers against selected country rules during registration, profile update, and checkout - Sanitize phone input in real time to filter out non-numeric characters
This commit is contained in:
parent
776e09a918
commit
917dd258d2
36
src/App.tsx
36
src/App.tsx
|
|
@ -2,9 +2,11 @@ import { useState, useEffect } from 'react';
|
|||
import axios from 'axios';
|
||||
|
||||
import { CURRICULUM_MODULES } from './constants/modules';
|
||||
import { COUNTRY_CODES } 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 } from './utils/validation';
|
||||
|
||||
// Modular Subcomponents
|
||||
import { StoreHeader } from './components/layout/StoreHeader';
|
||||
|
|
@ -211,13 +213,18 @@ export default function App() {
|
|||
calculatePricing();
|
||||
}, [cart, appliedCoupon, currency]);
|
||||
|
||||
// Update currency automatically on country selection during registration
|
||||
// Update currency and dial code automatically on country selection during registration
|
||||
useEffect(() => {
|
||||
if (authMode === 'register') {
|
||||
if (authCountry === 'India') {
|
||||
setCurrency('INR');
|
||||
setAuthDialCode('+91');
|
||||
} else {
|
||||
setCurrency('USD');
|
||||
const match = COUNTRY_CODES.find(c => c.country.toLowerCase() === authCountry.toLowerCase());
|
||||
if (match) {
|
||||
setAuthDialCode(match.code);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [authCountry, authMode]);
|
||||
|
|
@ -273,6 +280,15 @@ export default function App() {
|
|||
setProfileError(null);
|
||||
setProfileSuccess(false);
|
||||
|
||||
if (profilePhone && profilePhone.trim()) {
|
||||
const phoneError = validatePhoneNumber(profilePhone, undefined, profileCountry);
|
||||
if (phoneError) {
|
||||
setProfileError(phoneError);
|
||||
setProfileSaving(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await axios.put(`${API_BASE}/api/auth/profile`, {
|
||||
full_name: profileDisplayName,
|
||||
|
|
@ -298,6 +314,15 @@ export default function App() {
|
|||
setAuthLoading(true);
|
||||
setAuthError(null);
|
||||
|
||||
if (authMode === 'register' && authPhone && authPhone.trim()) {
|
||||
const phoneError = validatePhoneNumber(authPhone, authDialCode, authCountry);
|
||||
if (phoneError) {
|
||||
setAuthError(phoneError);
|
||||
setAuthLoading(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const endpoint = authMode === 'login' ? '/api/auth/login' : '/api/auth/register';
|
||||
const fullPhone = authPhone ? `${authDialCode}${authPhone}` : undefined;
|
||||
const payload = authMode === 'login'
|
||||
|
|
@ -604,9 +629,16 @@ export default function App() {
|
|||
setCheckoutLoading(false);
|
||||
return;
|
||||
}
|
||||
const cleanedPhone = cleanPhoneInput(inputPhone);
|
||||
const phoneError = validatePhoneNumber(cleanedPhone, undefined, loggedInUser?.country || 'India');
|
||||
if (phoneError) {
|
||||
alert(phoneError);
|
||||
setCheckoutLoading(false);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const res = await axios.put(`${API_BASE}/api/auth/profile`, {
|
||||
phone_number: inputPhone
|
||||
phone_number: cleanedPhone
|
||||
}, {
|
||||
headers: { 'Authorization': `Bearer ${userToken}` }
|
||||
});
|
||||
|
|
|
|||
|
|
@ -3,7 +3,8 @@ import {
|
|||
XCircle, Sparkles, AlertCircle, CheckCircle, RefreshCw,
|
||||
Phone, Globe, Eye, EyeOff, LogIn, UserPlus
|
||||
} from 'lucide-react';
|
||||
import { COUNTRY_CODES } from '../../constants/countries';
|
||||
import { COUNTRY_CODES, getCountryRuleByDialCode } from '../../constants/countries';
|
||||
import { cleanPhoneInput } from '../../utils/validation';
|
||||
|
||||
interface AuthModalProps {
|
||||
isOpen: boolean;
|
||||
|
|
@ -76,6 +77,7 @@ export const AuthModal: React.FC<AuthModalProps> = ({
|
|||
onResetPasswordSubmit,
|
||||
}) => {
|
||||
const [showPassword, setShowPassword] = useState(false);
|
||||
const activeRule = getCountryRuleByDialCode(authDialCode);
|
||||
|
||||
if (!isOpen) return null;
|
||||
|
||||
|
|
@ -290,7 +292,14 @@ export const AuthModal: React.FC<AuthModalProps> = ({
|
|||
<div className="relative">
|
||||
<select
|
||||
value={authDialCode}
|
||||
onChange={(e) => setAuthDialCode(e.target.value)}
|
||||
onChange={(e) => {
|
||||
const newCode = e.target.value;
|
||||
setAuthDialCode(newCode);
|
||||
const match = COUNTRY_CODES.find((c) => c.code === newCode);
|
||||
if (match && match.country) {
|
||||
setAuthCountry(match.country);
|
||||
}
|
||||
}}
|
||||
className="w-[90px] bg-slate-950 border border-slate-800 rounded-xl px-3 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500 appearance-none"
|
||||
>
|
||||
{COUNTRY_CODES.map((cc) => (
|
||||
|
|
@ -303,11 +312,19 @@ export const AuthModal: React.FC<AuthModalProps> = ({
|
|||
<input
|
||||
type="tel"
|
||||
value={authPhone}
|
||||
onChange={(e) => setAuthPhone(e.target.value)}
|
||||
placeholder="9876543210"
|
||||
onChange={(e) => setAuthPhone(cleanPhoneInput(e.target.value, activeRule.maxLength))}
|
||||
placeholder={activeRule.placeholder}
|
||||
maxLength={activeRule.maxLength}
|
||||
className="flex-1 bg-slate-950 border border-slate-800 rounded-xl px-4 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500 placeholder:text-slate-700"
|
||||
/>
|
||||
</div>
|
||||
<p className="text-[10px] text-slate-500 font-medium">
|
||||
{activeRule.country === 'India'
|
||||
? '🇮🇳 India: Enter 10-digit mobile number'
|
||||
: activeRule.minLength === activeRule.maxLength
|
||||
? `${activeRule.country}: Requires exactly ${activeRule.maxLength} digits`
|
||||
: `${activeRule.country}: ${activeRule.minLength} to ${activeRule.maxLength} digits`}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
|
|
@ -320,7 +337,14 @@ export const AuthModal: React.FC<AuthModalProps> = ({
|
|||
<div className="relative">
|
||||
<select
|
||||
value={authCountry}
|
||||
onChange={(e) => setAuthCountry(e.target.value)}
|
||||
onChange={(e) => {
|
||||
const newCountry = e.target.value;
|
||||
setAuthCountry(newCountry);
|
||||
const match = COUNTRY_CODES.find((c) => c.country.toLowerCase() === newCountry.toLowerCase());
|
||||
if (match) {
|
||||
setAuthDialCode(match.code);
|
||||
}
|
||||
}}
|
||||
className="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500 appearance-none"
|
||||
>
|
||||
<option value="India">India (Calculates in INR ₹)</option>
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import {
|
|||
Eye, EyeOff, RefreshCw, LogOut
|
||||
} from 'lucide-react';
|
||||
import type { UserProfile, ModuleItem, Currency } from '../../types';
|
||||
import { cleanPhoneInput } from '../../utils/validation';
|
||||
|
||||
interface ProfileDrawerProps {
|
||||
isOpen: boolean;
|
||||
|
|
@ -143,8 +144,9 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
|||
<input
|
||||
type="text"
|
||||
value={profilePhone}
|
||||
onChange={(e) => setProfilePhone(e.target.value)}
|
||||
onChange={(e) => setProfilePhone(cleanPhoneInput(e.target.value))}
|
||||
placeholder="+919876543210"
|
||||
maxLength={18}
|
||||
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500 focus:ring-1 focus:ring-indigo-500"
|
||||
/>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -2,22 +2,61 @@ export interface CountryCode {
|
|||
code: string;
|
||||
label: string;
|
||||
country: string;
|
||||
minLength: number;
|
||||
maxLength: number;
|
||||
placeholder: string;
|
||||
}
|
||||
|
||||
export const COUNTRY_CODES: CountryCode[] = [
|
||||
{ code: '+91', label: '🇮🇳 +91', country: 'India' },
|
||||
{ code: '+1', label: '🇺🇸 +1', country: 'US' },
|
||||
{ code: '+44', label: '🇬🇧 +44', country: 'UK' },
|
||||
{ code: '+61', label: '🇦🇺 +61', country: 'AU' },
|
||||
{ code: '+49', label: '🇩🇪 +49', country: 'DE' },
|
||||
{ code: '+33', label: '🇫🇷 +33', country: 'FR' },
|
||||
{ code: '+81', label: '🇯🇵 +81', country: 'JP' },
|
||||
{ code: '+86', label: '🇨🇳 +86', country: 'CN' },
|
||||
{ code: '+971', label: '🇦🇪 +971', country: 'UAE' },
|
||||
{ code: '+65', label: '🇸🇬 +65', country: 'SG' },
|
||||
{ code: '+55', label: '🇧🇷 +55', country: 'BR' },
|
||||
{ code: '+82', label: '🇰🇷 +82', country: 'KR' },
|
||||
{ code: '+7', label: '🇷🇺 +7', country: 'RU' },
|
||||
{ code: '+27', label: '🇿🇦 +27', country: 'ZA' },
|
||||
{ code: '+234', label: '🇳🇬 +234', country: 'NG' },
|
||||
{ code: '+91', label: '🇮🇳 +91', country: 'India', minLength: 10, maxLength: 10, placeholder: '10-digit mobile number' },
|
||||
{ code: '+1', label: '🇺🇸 +1', country: 'United States', minLength: 10, maxLength: 10, placeholder: '10-digit phone number' },
|
||||
{ code: '+44', label: '🇬🇧 +44', country: 'United Kingdom', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
||||
{ code: '+61', label: '🇦🇺 +61', country: 'Australia', minLength: 9, maxLength: 10, placeholder: '9 to 10 digits' },
|
||||
{ code: '+49', label: '🇩🇪 +49', country: 'Germany', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
||||
{ code: '+33', label: '🇫🇷 +33', country: 'France', minLength: 9, maxLength: 10, placeholder: '9 to 10 digits' },
|
||||
{ code: '+81', label: '🇯🇵 +81', country: 'Japan', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
||||
{ code: '+86', label: '🇨🇳 +86', country: 'China', minLength: 11, maxLength: 11, placeholder: '11-digit mobile number' },
|
||||
{ code: '+971', label: '🇦🇪 +971', country: 'UAE', minLength: 9, maxLength: 9, placeholder: '9-digit mobile number' },
|
||||
{ code: '+65', label: '🇸🇬 +65', country: 'Singapore', minLength: 8, maxLength: 8, placeholder: '8-digit mobile number' },
|
||||
{ code: '+55', label: '🇧🇷 +55', country: 'Brazil', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
||||
{ code: '+82', label: '🇰🇷 +82', country: 'South Korea', minLength: 9, maxLength: 11, placeholder: '9 to 11 digits' },
|
||||
{ code: '+7', label: '🇷🇺 +7', country: 'Russia', minLength: 10, maxLength: 10, placeholder: '10-digit mobile number' },
|
||||
{ code: '+27', label: '🇿🇦 +27', country: 'South Africa', minLength: 9, maxLength: 10, placeholder: '9 to 10 digits' },
|
||||
{ code: '+234', label: '🇳🇬 +234', country: 'Nigeria', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
||||
];
|
||||
|
||||
export const getCountryRuleByDialCode = (dialCode: string): CountryCode => {
|
||||
return (
|
||||
COUNTRY_CODES.find((c) => c.code === dialCode) || {
|
||||
code: dialCode,
|
||||
label: dialCode,
|
||||
country: 'Other',
|
||||
minLength: 6,
|
||||
maxLength: 15,
|
||||
placeholder: 'Phone number',
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export const getCountryRuleByName = (countryName: string): CountryCode => {
|
||||
if (!countryName) return COUNTRY_CODES[0];
|
||||
const normalized = countryName.toLowerCase();
|
||||
return (
|
||||
COUNTRY_CODES.find(
|
||||
(c) =>
|
||||
c.country.toLowerCase() === normalized ||
|
||||
(normalized.includes('india') && c.country === 'India') ||
|
||||
(normalized.includes('united states') && c.country === 'United States') ||
|
||||
(normalized.includes('united kingdom') && c.country === 'United Kingdom') ||
|
||||
(normalized.includes('germany') && c.country === 'Germany') ||
|
||||
(normalized.includes('australia') && c.country === 'Australia')
|
||||
) || {
|
||||
code: '+91',
|
||||
label: '🇮🇳 +91',
|
||||
country: countryName,
|
||||
minLength: 6,
|
||||
maxLength: 15,
|
||||
placeholder: 'Phone number',
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -0,0 +1,76 @@
|
|||
import { getCountryRuleByDialCode, getCountryRuleByName } from '../constants/countries';
|
||||
|
||||
/**
|
||||
* Strips non-numeric / non-phone characters from text in real-time
|
||||
* If maxLength is provided, strictly restricts digit length
|
||||
*/
|
||||
export const cleanPhoneInput = (text: string, maxLength?: number): string => {
|
||||
const trimmedStart = text.trimStart();
|
||||
const hasPlus = trimmedStart.startsWith('+');
|
||||
const rest = hasPlus ? trimmedStart.slice(1) : text;
|
||||
const digitsOnly = rest.replace(/\D/g, '');
|
||||
const boundedDigits = maxLength ? digitsOnly.slice(0, maxLength) : digitsOnly;
|
||||
return hasPlus ? `+${boundedDigits}` : boundedDigits;
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates phone number according to country rules (e.g. India = exactly 10 digits)
|
||||
*/
|
||||
export const validatePhoneNumber = (
|
||||
val?: string | null,
|
||||
dialCode?: string,
|
||||
country?: string
|
||||
): string | null => {
|
||||
if (!val || !val.trim()) return null;
|
||||
const trimmed = val.trim();
|
||||
const digits = trimmed.replace(/\D/g, '');
|
||||
|
||||
if (digits.length === 0) {
|
||||
return 'Please enter a valid phone number with numeric digits.';
|
||||
}
|
||||
|
||||
// Resolve country rule by dial code or country name
|
||||
let rule = dialCode ? getCountryRuleByDialCode(dialCode) : undefined;
|
||||
if ((!rule || rule.country === 'Other') && country) {
|
||||
rule = getCountryRuleByName(country);
|
||||
}
|
||||
// Default to India if no specific rule resolved and dial code is +91 or country is India
|
||||
if (!rule || rule.country === 'Other') {
|
||||
if (dialCode === '+91' || (country && country.toLowerCase() === 'india')) {
|
||||
rule = getCountryRuleByDialCode('+91');
|
||||
}
|
||||
}
|
||||
|
||||
if (rule && rule.country !== 'Other') {
|
||||
if (rule.minLength === rule.maxLength) {
|
||||
if (digits.length !== rule.minLength) {
|
||||
return `${rule.country} phone number must be exactly ${rule.minLength} digits.`;
|
||||
}
|
||||
} else {
|
||||
if (digits.length < rule.minLength) {
|
||||
return `${rule.country} phone number must be at least ${rule.minLength} digits.`;
|
||||
}
|
||||
if (digits.length > rule.maxLength) {
|
||||
return `${rule.country} phone number cannot exceed ${rule.maxLength} digits.`;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// General fallback ITU E.164
|
||||
if (digits.length < 6) {
|
||||
return 'Phone number must contain at least 6 digits.';
|
||||
}
|
||||
if (digits.length > 15) {
|
||||
return 'Phone number cannot exceed 15 digits.';
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
export const isValidPhoneNumber = (
|
||||
val?: string | null,
|
||||
dialCode?: string,
|
||||
country?: string
|
||||
): boolean => {
|
||||
return validatePhoneNumber(val, dialCode, country) === null;
|
||||
};
|
||||
Loading…
Reference in New Issue