fix(auth): normalize mobile digits and whitespace in login, register, and admin user creation
Deploy Canina / deploy (push) Successful in 1m43s
Deploy Canina / deploy (push) Successful in 1m43s
This commit is contained in:
1 parent
2984b9f637
commit
e1f471c978
3 files changed
+69
-21
No files matched your search
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Normalizes Iranian phone numbers:
|
||||
* 1. Converts Persian and Arabic digits to English digits
|
||||
* 2. Removes spaces, dashes, parentheses
|
||||
* 3. Converts +98 or 0098 prefixes to 0
|
||||
*/
|
||||
export function normalizeMobile(mobile: string | undefined | null): string {
|
||||
if (!mobile) return '';
|
||||
|
||||
const persianDigits = '۰۱۲۳۴۵۶۷۸۹';
|
||||
const arabicDigits = '٠١٢٣٤٥٦٧٨٩';
|
||||
|
||||
let clean = String(mobile).trim();
|
||||
|
||||
// Convert Persian & Arabic numbers
|
||||
for (let i = 0; i < 10; i++) {
|
||||
clean = clean.replace(new RegExp(persianDigits[i], 'g'), String(i));
|
||||
clean = clean.replace(new RegExp(arabicDigits[i], 'g'), String(i));
|
||||
}
|
||||
|
||||
// Remove non-digits except +
|
||||
clean = clean.replace(/[^\d+]/g, '');
|
||||
|
||||
// Normalize prefix
|
||||
if (clean.startsWith('+98')) {
|
||||
clean = '0' + clean.slice(3);
|
||||
} else if (clean.startsWith('0098')) {
|
||||
clean = '0' + clean.slice(4);
|
||||
} else if (clean.startsWith('98') && clean.length === 12) {
|
||||
clean = '0' + clean.slice(2);
|
||||
} else if (clean.length === 10 && !clean.startsWith('0')) {
|
||||
clean = '0' + clean;
|
||||
}
|
||||
|
||||
return clean;
|
||||
}
|
||||
Reference in new issue
Block a user