From 2513c854117f0641569d2b8acea4a4aa25f186aa Mon Sep 17 00:00:00 2001 From: Urvish Date: Wed, 11 Nov 2026 13:21:45 +0530 Subject: [PATCH] fix(auth): enforce strict national phone length and prevent country code combining --- src/components/auth/ProfileDrawer.tsx | 3 ++- src/components/cart/CartDrawer.tsx | 11 +++++--- src/components/common/PhoneInput.tsx | 38 +++++++++++++++++++++++++-- src/utils/validation.ts | 27 +++++++++++++------ 4 files changed, 65 insertions(+), 14 deletions(-) diff --git a/src/components/auth/ProfileDrawer.tsx b/src/components/auth/ProfileDrawer.tsx index ce10da9..b243f48 100644 --- a/src/components/auth/ProfileDrawer.tsx +++ b/src/components/auth/ProfileDrawer.tsx @@ -92,7 +92,7 @@ export const ProfileDrawer: React.FC = ({ const handleLocalPhoneChange = (newLocal: string) => { setLocalPhone(newLocal); - setProfilePhone(formatE164Phone(dialCode, newLocal)); + setProfilePhone(newLocal ? formatE164Phone(dialCode, newLocal) : ''); }; const handleDialCodeChange = (newCode: string, newCountry: string) => { @@ -192,6 +192,7 @@ export const ProfileDrawer: React.FC = ({ onPhoneChange={handleLocalPhoneChange} theme="light" disabled={profileSaving} + error={profileError && profileError.toLowerCase().includes('phone') ? profileError : null} /> diff --git a/src/components/cart/CartDrawer.tsx b/src/components/cart/CartDrawer.tsx index 845920f..e936b21 100644 --- a/src/components/cart/CartDrawer.tsx +++ b/src/components/cart/CartDrawer.tsx @@ -1,6 +1,7 @@ import React from 'react'; import { ShoppingCart, ShoppingBag, Trash2, RefreshCw, CreditCard, XCircle } from 'lucide-react'; import type { ModuleItem, Currency, UserProfile, VerificationResult } from '../../types'; +import { isValidPhoneNumber } from '../../utils/validation'; interface CartDrawerProps { cart: ModuleItem[]; @@ -351,12 +352,12 @@ export const CartDrawer: React.FC = ({ Billing Profile - {!loggedInUser.phone_number && ( + {(!loggedInUser.phone_number || !isValidPhoneNumber(loggedInUser.phone_number, undefined, loggedInUser.country || 'India')) && ( )} @@ -368,7 +369,11 @@ export const CartDrawer: React.FC = ({
Phone: - {loggedInUser.phone_number || ( + {loggedInUser.phone_number && isValidPhoneNumber(loggedInUser.phone_number, undefined, loggedInUser.country || 'India') ? ( + loggedInUser.phone_number + ) : loggedInUser.phone_number ? ( + Incomplete ({loggedInUser.phone_number}) + ) : ( Required for checkout )} diff --git a/src/components/common/PhoneInput.tsx b/src/components/common/PhoneInput.tsx index 9cb7dd3..311cddb 100644 --- a/src/components/common/PhoneInput.tsx +++ b/src/components/common/PhoneInput.tsx @@ -50,8 +50,38 @@ export const PhoneInput: React.FC = ({ }; const handlePhoneInputChange = (e: React.ChangeEvent) => { - // Strictly numeric, capped at maxLength - const cleaned = cleanPhoneInput(e.target.value, activeRule.maxLength, false); + const inputVal = e.target.value; + const trimmed = inputVal.trim(); + + // Check if input contains an explicit international prefix '+' + if (trimmed.startsWith('+')) { + const sorted = [...COUNTRY_CODES].sort((a, b) => b.code.length - a.code.length); + const matched = sorted.find((c) => trimmed.startsWith(c.code)); + if (matched) { + if (onDialCodeChange && matched.code !== dialCode) { + onDialCodeChange(matched.code, matched.country); + } + const rest = trimmed.slice(matched.code.length).replace(/\D/g, ''); + onPhoneChange(rest.slice(0, matched.maxLength)); + return; + } + } + + // Check if input was pasted with dial code digits without '+' (e.g. 919876543210) + const dialDigits = activeRule.code.replace(/\D/g, ''); + const rawDigits = trimmed.replace(/\D/g, ''); + if ( + dialDigits && + rawDigits.startsWith(dialDigits) && + rawDigits.length > activeRule.maxLength + ) { + const rest = rawDigits.slice(dialDigits.length); + onPhoneChange(rest.slice(0, activeRule.maxLength)); + return; + } + + // Strictly numeric local digits, capped at active country's maxLength + const cleaned = cleanPhoneInput(inputVal, activeRule.maxLength, false); onPhoneChange(cleaned); }; @@ -63,6 +93,10 @@ export const PhoneInput: React.FC = ({ ) { return; } + // Allow '+' as leading character to support international number paste/typing + if (e.key === '+' && (e.currentTarget.selectionStart === 0 || !phone)) { + return; + } // Block any non-digit if (!/^\d$/.test(e.key)) { e.preventDefault(); diff --git a/src/utils/validation.ts b/src/utils/validation.ts index cf26d48..0e904b0 100644 --- a/src/utils/validation.ts +++ b/src/utils/validation.ts @@ -51,6 +51,11 @@ export const validatePhoneNumber = ( if ((!rule || rule.country === 'Other') && country) { rule = getCountryRuleByName(country); } + // If still not found and input starts with '+', match from COUNTRY_CODES + if (!rule && trimmed.startsWith('+')) { + const sorted = [...COUNTRY_CODES].sort((a, b) => b.code.length - a.code.length); + rule = sorted.find((c) => trimmed.startsWith(c.code)); + } // 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')) { @@ -62,12 +67,14 @@ export const validatePhoneNumber = ( const dialDigits = rule.code.replace(/\D/g, ''); let localDigits = allDigits; - // If formatted with country code (e.g. +919876543210 or 919876543210 where total length > rule.maxLength), strip country prefix - if ( - (trimmed.startsWith('+') || allDigits.length > rule.maxLength) && - allDigits.startsWith(dialDigits) && - allDigits.length === dialDigits.length + rule.minLength - ) { + if (trimmed.startsWith('+')) { + // Input includes country code prefix (e.g. +919876543210 or +9198765432) + // Must strip the dial code to extract the actual local number + if (allDigits.startsWith(dialDigits)) { + localDigits = allDigits.slice(dialDigits.length); + } + } else if (allDigits.length > rule.maxLength && allDigits.startsWith(dialDigits)) { + // Input was formatted with dial digits without '+' and exceeds local maxLength (e.g. 919876543210) localDigits = allDigits.slice(dialDigits.length); } @@ -137,11 +144,15 @@ export const parsePhoneNumber = ( }; } - // If no '+' prefix, check if digits start with fallback country dial digits + // If no '+' prefix, check if digits start with fallback country dial digits and exceed national length const dialDigits = defaultRule.code.replace(/\D/g, ''); const rawDigits = trimmed.replace(/\D/g, ''); let local = rawDigits; - if (rawDigits.startsWith(dialDigits) && rawDigits.length > dialDigits.length) { + if ( + dialDigits && + rawDigits.startsWith(dialDigits) && + rawDigits.length > defaultRule.maxLength + ) { local = rawDigits.slice(dialDigits.length); } return {