fix(auth): enforce strict national phone length and prevent country code combining

This commit is contained in:
Urvish Prajapati 2026-11-11 13:21:45 +05:30
parent 62c7a4aead
commit 2513c85411
4 changed files with 65 additions and 14 deletions

View File

@ -92,7 +92,7 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
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<ProfileDrawerProps> = ({
onPhoneChange={handleLocalPhoneChange}
theme="light"
disabled={profileSaving}
error={profileError && profileError.toLowerCase().includes('phone') ? profileError : null}
/>
</div>

View File

@ -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<CartDrawerProps> = ({
<span className="text-[10px] uppercase font-black tracking-wider text-slate-400">
Billing Profile
</span>
{!loggedInUser.phone_number && (
{(!loggedInUser.phone_number || !isValidPhoneNumber(loggedInUser.phone_number, undefined, loggedInUser.country || 'India')) && (
<button
onClick={onUpdateBillingPhone}
className="text-[9px] text-indigo-600 hover:text-indigo-500 font-black uppercase tracking-wider underline focus:outline-none"
>
Add Number
{loggedInUser.phone_number ? 'Update Number' : 'Add Number'}
</button>
)}
</div>
@ -368,7 +369,11 @@ export const CartDrawer: React.FC<CartDrawerProps> = ({
<div className="flex justify-between text-xs font-semibold text-slate-600">
<span>Phone:</span>
<span className="text-slate-800 font-bold">
{loggedInUser.phone_number || (
{loggedInUser.phone_number && isValidPhoneNumber(loggedInUser.phone_number, undefined, loggedInUser.country || 'India') ? (
loggedInUser.phone_number
) : loggedInUser.phone_number ? (
<span className="text-amber-600 font-black italic">Incomplete ({loggedInUser.phone_number})</span>
) : (
<span className="text-rose-500 font-black italic">Required for checkout</span>
)}
</span>

View File

@ -50,8 +50,38 @@ export const PhoneInput: React.FC<PhoneInputProps> = ({
};
const handlePhoneInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
// 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<PhoneInputProps> = ({
) {
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();

View File

@ -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 {