-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
56 lines (51 loc) · 2.42 KB
/
utils.ts
File metadata and controls
56 lines (51 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
export const getInitials = (name: string) => {
const parts = name.trim().split(/\s+/);
if (parts.length === 0) return '';
if (parts.length === 1) return parts[0].substring(0, 2).toUpperCase();
return (parts[0][0] + parts[parts.length - 1][0]).toUpperCase();
};
export const formatName = (name: string) => {
const parts = name.trim().split(/\s+/);
if (parts.length < 2) return name;
// Return full First Name and Last Name (and middle names if present)
// User request: "do not abbreviate first name"
return parts.join(' ');
};
export const getFriendlyErrorMessage = (error: any): string => {
// If it's not a string or object with message, return generic
if (!error) return "An unexpected error occurred.";
const message = typeof error === 'string' ? error : (error.message || '');
const code = error.code || '';
// Handle specific Firebase Auth codes
switch (code) {
case 'auth/user-not-found':
case 'auth/invalid-credential':
case 'auth/wrong-password':
return "Incorrect email or password. Please try again.";
case 'auth/email-already-in-use':
return "This email address is already registered. Please sign in instead.";
case 'auth/weak-password':
return "Password should be at least 6 characters long.";
case 'auth/invalid-email':
return "Please enter a valid email address.";
case 'auth/too-many-requests':
return "Too many failed attempts. Please try again later.";
case 'auth/network-request-failed':
return "Network error. Please check your internet connection.";
case 'permission-denied':
return "You do not have permission to perform this action.";
case 'unavailable':
return "The service is temporarily unavailable.";
default:
// Fallback: Clean up "Firebase: Error (auth/foo)." -> "auth/foo" -> better text?
// Or just return the cleaner message.
if (message.includes('billing-not-enabled')) {
return "Phone authentication is currently unavailable (Billing required).";
}
// Remove "Firebase: " prefix and "Error (code)." wrapper if present
return message
.replace(/^Firebase:\s*/, '')
.replace(/^Error\s*\(([^)]+)\)\.?\s*$/, '$1')
.replace(/\.$/, ''); // Remove trailing dot
}
};