@umituz/react-native-firebase 2.6.1 → 2.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/package.json +1 -1
  2. package/src/application/auth/ports/AuthPort_part_aa +150 -0
  3. package/src/application/auth/ports/AuthPort_part_ab +14 -0
  4. package/src/application/auth/use-cases/SignInUseCaseHelpers.ts +0 -0
  5. package/src/application/auth/use-cases/SignInUseCaseMain.ts +0 -0
  6. package/src/application/auth/use-cases/SignInUseCase_part_aa +150 -0
  7. package/src/application/auth/use-cases/SignInUseCase_part_ab +103 -0
  8. package/src/application/auth/use-cases/SignOutUseCaseCleanup.ts +0 -0
  9. package/src/application/auth/use-cases/SignOutUseCaseMain.ts +0 -0
  10. package/src/application/auth/use-cases/SignOutUseCase_part_aa +150 -0
  11. package/src/application/auth/use-cases/SignOutUseCase_part_ab +138 -0
  12. package/src/domains/account-deletion/domain/services/UserValidationHelpers.ts.bak +181 -0
  13. package/src/domains/account-deletion/domain/services/UserValidationHelpers_part_aa +150 -0
  14. package/src/domains/account-deletion/domain/services/UserValidationHelpers_part_ab +31 -0
  15. package/src/domains/account-deletion/domain/services/{UserValidationService.ts → UserValidationService.ts.bak} +1 -10
  16. package/src/domains/account-deletion/domain/services/UserValidationService_part_aa +150 -0
  17. package/src/domains/account-deletion/domain/services/UserValidationService_part_ab +136 -0
  18. package/src/domains/account-deletion/infrastructure/services/AccountDeletionExecutor_part_aa +150 -0
  19. package/src/domains/account-deletion/infrastructure/services/AccountDeletionExecutor_part_ab +80 -0
  20. package/src/domains/account-deletion/infrastructure/services/AccountDeletionReauthHandler_part_aa +150 -0
  21. package/src/domains/account-deletion/infrastructure/services/AccountDeletionReauthHandler_part_ab +24 -0
  22. package/src/domains/account-deletion/infrastructure/services/AccountDeletionRepository_part_aa +150 -0
  23. package/src/domains/account-deletion/infrastructure/services/AccountDeletionRepository_part_ab +116 -0
  24. package/src/domains/account-deletion/infrastructure/services/reauthentication.service_part_aa +150 -0
  25. package/src/domains/account-deletion/infrastructure/services/reauthentication.service_part_ab +10 -0
  26. package/src/domains/auth/infrastructure_part_aa +150 -0
  27. package/src/domains/auth/infrastructure_part_ab +6 -0
  28. package/src/domains/auth/presentation/hooks/GoogleOAuthHelpers.ts +0 -0
  29. package/src/domains/auth/presentation/hooks/GoogleOAuthHookService_part_aa +150 -0
  30. package/src/domains/auth/presentation/hooks/GoogleOAuthHookService_part_ab +97 -0
  31. package/src/domains/auth/presentation/hooks/GoogleOAuthService.ts +0 -0
  32. package/src/domains/firestore/domain/entities/Collection.ts +31 -191
  33. package/src/domains/firestore/domain/entities/Collection.ts.bak +288 -0
  34. package/src/domains/firestore/domain/entities/CollectionFactory.ts +55 -0
  35. package/src/domains/firestore/domain/entities/CollectionHelpers.ts +143 -0
  36. package/src/domains/firestore/domain/entities/CollectionUtils.ts +72 -0
  37. package/src/domains/firestore/domain/entities/CollectionValidation.ts +138 -0
  38. package/src/domains/firestore/domain/entities/Collection_part_aa +150 -0
  39. package/src/domains/firestore/domain/entities/Collection_part_ab +138 -0
  40. package/src/domains/firestore/domain/entities/DocumentHelpers.ts +0 -0
  41. package/src/domains/firestore/domain/entities/DocumentMain.ts +0 -0
  42. package/src/domains/firestore/domain/entities/Document_part_aa +150 -0
  43. package/src/domains/firestore/domain/entities/Document_part_ab +83 -0
  44. package/src/domains/firestore/domain/index.ts +44 -9
  45. package/src/domains/firestore/domain/services/QueryServiceAnalysis_part_aa +150 -0
  46. package/src/domains/firestore/domain/services/QueryServiceAnalysis_part_ab +19 -0
  47. package/src/domains/firestore/domain/services/QueryServiceHelpers_part_aa +150 -0
  48. package/src/domains/firestore/domain/services/QueryServiceHelpers_part_ab +1 -0
  49. package/src/domains/firestore/domain/services/QueryService_part_aa +150 -0
  50. package/src/domains/firestore/domain/services/QueryService_part_ab +32 -0
  51. package/src/domains/firestore/domain/value-objects/QueryOptions.ts.bak +6 -135
  52. package/src/domains/firestore/domain/value-objects/QueryOptionsSerialization_part_aa +150 -0
  53. package/src/domains/firestore/domain/value-objects/QueryOptionsSerialization_part_ab +57 -0
  54. package/src/domains/firestore/domain/value-objects/QueryOptionsValidation_part_aa +150 -0
  55. package/src/domains/firestore/domain/value-objects/QueryOptionsValidation_part_ab +32 -0
  56. package/src/domains/firestore/domain/value-objects/{QueryOptions.ts → QueryOptions_part_aa} +0 -41
  57. package/src/domains/firestore/domain/value-objects/QueryOptions_part_ab +41 -0
  58. package/src/domains/firestore/domain/value-objects/WhereClauseFactory_part_aa +150 -0
  59. package/src/domains/firestore/domain/value-objects/WhereClauseFactory_part_ab +57 -0
  60. package/src/domains/firestore/domain/value-objects/WhereClause_part_aa +150 -0
  61. package/src/domains/firestore/domain/value-objects/WhereClause_part_ab +149 -0
  62. package/src/shared/infrastructure/base/ErrorHandler_part_aa +150 -0
  63. package/src/shared/infrastructure/base/ErrorHandler_part_ab +39 -0
  64. package/src/shared/infrastructure/base/ServiceBase_part_aa +150 -0
  65. package/src/shared/infrastructure/base/ServiceBase_part_ab +70 -0
  66. package/src/shared/infrastructure/config/base/ServiceClientSingleton_part_aa +150 -0
  67. package/src/shared/infrastructure/config/base/ServiceClientSingleton_part_ab +5 -0
  68. /package/src/application/auth/ports/{AuthPort.ts → AuthPort.ts.bak} +0 -0
  69. /package/src/application/auth/use-cases/{SignInUseCase.ts → SignInUseCase.ts.bak} +0 -0
  70. /package/src/application/auth/use-cases/{SignOutUseCase.ts → SignOutUseCase.ts.bak} +0 -0
  71. /package/src/domains/account-deletion/infrastructure/services/{AccountDeletionExecutor.ts → AccountDeletionExecutor.ts.bak} +0 -0
  72. /package/src/domains/account-deletion/infrastructure/services/{AccountDeletionReauthHandler.ts → AccountDeletionReauthHandler.ts.bak} +0 -0
  73. /package/src/domains/account-deletion/infrastructure/services/{AccountDeletionRepository.ts → AccountDeletionRepository.ts.bak} +0 -0
  74. /package/src/domains/account-deletion/infrastructure/services/{reauthentication.service.ts → reauthentication.service.ts.bak} +0 -0
  75. /package/src/domains/auth/{infrastructure.ts → infrastructure.ts.bak} +0 -0
  76. /package/src/domains/auth/presentation/hooks/{GoogleOAuthHookService.ts → GoogleOAuthHookService.ts.bak} +0 -0
  77. /package/src/domains/firestore/domain/entities/{Document.ts → Document.ts.bak} +0 -0
  78. /package/src/domains/firestore/domain/services/{QueryService.ts → QueryService.ts.bak} +0 -0
  79. /package/src/domains/firestore/domain/services/{QueryServiceAnalysis.ts → QueryServiceAnalysis.ts.bak} +0 -0
  80. /package/src/domains/firestore/domain/services/{QueryServiceHelpers.ts → QueryServiceHelpers.ts.bak} +0 -0
  81. /package/src/domains/firestore/domain/value-objects/{QueryOptionsSerialization.ts → QueryOptionsSerialization.ts.bak} +0 -0
  82. /package/src/domains/firestore/domain/value-objects/{QueryOptionsValidation.ts → QueryOptionsValidation.ts.bak} +0 -0
  83. /package/src/domains/firestore/domain/value-objects/{WhereClause.ts → WhereClause.ts.bak} +0 -0
  84. /package/src/domains/firestore/domain/value-objects/{WhereClauseFactory.ts → WhereClauseFactory.ts.bak} +0 -0
  85. /package/src/shared/infrastructure/base/{ErrorHandler.ts → ErrorHandler.ts.bak} +0 -0
  86. /package/src/shared/infrastructure/base/{ServiceBase.ts → ServiceBase.ts.bak} +0 -0
  87. /package/src/shared/infrastructure/config/base/{ServiceClientSingleton.ts → ServiceClientSingleton.ts.bak} +0 -0
@@ -0,0 +1,138 @@
1
+ error: {
2
+ code: 'auth/not-signed-in',
3
+ message: 'No user is currently signed in',
4
+ },
5
+ };
6
+ }
7
+
8
+ // Additional checks can be added here
9
+ // For example: check for unsaved changes, active operations, etc.
10
+
11
+ return { success: true };
12
+ }
13
+
14
+ /**
15
+ * Sign out with confirmation
16
+ * Shows confirmation dialog before signing out
17
+ */
18
+ async signOutWithConfirmation(): Promise<SignOutUseCaseResult> {
19
+ const confirmed = await this.confirmSignOut();
20
+
21
+ if (!confirmed) {
22
+ return {
23
+ success: false,
24
+ error: {
25
+ code: 'auth/sign-out-cancelled',
26
+ message: 'Sign out was cancelled',
27
+ },
28
+ wasSignedIn: this.isSignedIn(),
29
+ };
30
+ }
31
+
32
+ return this.execute();
33
+ }
34
+
35
+ /**
36
+ * Sign out and navigate
37
+ * Signs out and navigates to sign in screen
38
+ */
39
+ async signOutAndNavigate(): Promise<SignOutUseCaseResult> {
40
+ return this.execute({
41
+ clearLocalData: true,
42
+ navigateToSignIn: true,
43
+ });
44
+ }
45
+
46
+ /**
47
+ * Force sign out
48
+ * Signs out even if there are errors
49
+ */
50
+ async forceSignOut(): Promise<void> {
51
+ try {
52
+ await this.execute({
53
+ clearLocalData: true,
54
+ });
55
+ } catch (error) {
56
+ // Ignore errors during force sign out
57
+ if (__DEV__) {
58
+ console.error('[SignOutUseCase] Force sign out error:', error);
59
+ }
60
+ }
61
+
62
+ // Ensure local data is cleared
63
+ await this.clearLocalData();
64
+ }
65
+
66
+ /**
67
+ * Get sign out statistics
68
+ */
69
+ getSignOutStats(): {
70
+ readonly isSignedIn: boolean;
71
+ readonly isAnonymous: boolean;
72
+ readonly hasUserId: boolean;
73
+ } {
74
+ return {
75
+ isSignedIn: this.isSignedIn(),
76
+ isAnonymous: this.isAnonymous(),
77
+ hasUserId: this.getCurrentUserId() !== null,
78
+ };
79
+ }
80
+
81
+ /**
82
+ * Create sign out options
83
+ */
84
+ createOptions(options: Partial<SignOutOptions> = {}): SignOutOptions {
85
+ return {
86
+ clearLocalData: false,
87
+ navigateToSignIn: false,
88
+ showConfirmation: false,
89
+ ...options,
90
+ };
91
+ }
92
+
93
+ /**
94
+ * Quick sign out helper
95
+ */
96
+ async quickSignOut(): Promise<SignOutUseCaseResult> {
97
+ return this.execute();
98
+ }
99
+
100
+ /**
101
+ * Check if confirmation should be shown
102
+ */
103
+ shouldShowConfirmation(): boolean {
104
+ // Default: show confirmation for authenticated users
105
+ return this.isSignedIn() && !this.isAnonymous();
106
+ }
107
+
108
+ /**
109
+ * Get user-friendly sign out message
110
+ */
111
+ getSignOutMessage(): string {
112
+ if (!this.isSignedIn()) {
113
+ return 'You are not signed in';
114
+ }
115
+
116
+ if (this.isAnonymous()) {
117
+ return 'Sign out from guest session?';
118
+ }
119
+
120
+ return 'Are you sure you want to sign out?';
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Factory function to create sign out use case
126
+ */
127
+ export function createSignOutUseCase(authPort: IAuthPort): SignOutUseCase {
128
+ return new SignOutUseCase(authPort);
129
+ }
130
+
131
+ /**
132
+ * Factory function to create sign out use case with default auth port
133
+ */
134
+ export async function createDefaultSignOutUseCase(): Promise<SignOutUseCase> {
135
+ const { createAuthPort } = await import('../ports/AuthPort');
136
+ const authPort = createAuthPort();
137
+ return createSignOutUseCase(authPort);
138
+ }
@@ -0,0 +1,181 @@
1
+ /**
2
+ * User Validation Helpers
3
+ * Single Responsibility: Provide helper methods for user validation
4
+ *
5
+ * Helper methods separated from main UserValidationService.
6
+ * Provides utility functions for user state checking.
7
+ *
8
+ * Max lines: 150 (enforced for maintainability)
9
+ */
10
+
11
+ import type { User } from 'firebase/auth';
12
+ import type { UserValidationService } from './UserValidationService';
13
+
14
+ /**
15
+ * Check if user email matches pattern
16
+ */
17
+ export function isValidEmailFormat(email: string): boolean {
18
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
19
+ return emailRegex.test(email);
20
+ }
21
+
22
+ /**
23
+ * Get password strength indicator
24
+ */
25
+ export function getPasswordStrength(password: string): 'weak' | 'medium' | 'strong' {
26
+ if (password.length < 6) {
27
+ return 'weak';
28
+ }
29
+
30
+ if (password.length < 8) {
31
+ return 'medium';
32
+ }
33
+
34
+ // Check for variety
35
+ const hasLower = /[a-z]/.test(password);
36
+ const hasUpper = /[A-Z]/.test(password);
37
+ const hasNumber = /[0-9]/.test(password);
38
+ const hasSpecial = /[^a-zA-Z0-9]/.test(password);
39
+
40
+ const varietyScore = [hasLower, hasUpper, hasNumber, hasSpecial].filter(Boolean).length;
41
+
42
+ if (varietyScore >= 3) {
43
+ return 'strong';
44
+ }
45
+
46
+ return 'medium';
47
+ }
48
+
49
+ /**
50
+ * Check if account is locked
51
+ */
52
+ export function isAccountLocked(errorCode?: string): boolean {
53
+ return errorCode === 'auth/too-many-requests' || errorCode === 'auth/user-disabled';
54
+ }
55
+
56
+ /**
57
+ * Get user-friendly error message
58
+ */
59
+ export function getAuthErrorMessage(errorCode: string): string {
60
+ const errorMessages: Record<string, string> = {
61
+ 'auth/invalid-credentials': 'Invalid email or password',
62
+ 'auth/user-not-found': 'No account found with this email',
63
+ 'auth/wrong-password': 'Incorrect password',
64
+ 'auth/unverified-email': 'Please verify your email first',
65
+ 'auth/user-disabled': 'This account has been disabled',
66
+ 'auth/too-many-requests': 'Too many attempts. Please try again later',
67
+ 'auth/invalid-email': 'Invalid email address',
68
+ };
69
+
70
+ return errorMessages[errorCode] || 'Authentication failed. Please try again.';
71
+ }
72
+
73
+ /**
74
+ * Check if user session is valid
75
+ */
76
+ export function isSessionValid(user: User, maxAgeMs: number = 30 * 60 * 1000): boolean {
77
+ if (!user.metadata.lastSignInTime) {
78
+ return false;
79
+ }
80
+
81
+ const lastSignIn = new Date(user.metadata.lastSignInTime).getTime();
82
+ const timeSinceSignIn = Date.now() - lastSignIn;
83
+
84
+ return timeSinceSignIn <= maxAgeMs;
85
+ }
86
+
87
+ /**
88
+ * Check if user needs to refresh session
89
+ */
90
+ export function needsSessionRefresh(user: User, maxAgeMs: number = 30 * 60 * 1000): boolean {
91
+ return !isSessionValid(user, maxAgeMs);
92
+ }
93
+
94
+ /**
95
+ * Get user display name
96
+ */
97
+ export function getUserDisplayName(user: User): string {
98
+ if (user.displayName) {
99
+ return user.displayName;
100
+ }
101
+
102
+ if (user.email) {
103
+ return user.email.split('@')[0];
104
+ }
105
+
106
+ return 'User';
107
+ }
108
+
109
+ /**
110
+ * Get user initials for avatar
111
+ */
112
+ export function getUserInitials(user: User): string {
113
+ if (user.displayName) {
114
+ const names = user.displayName.split(' ');
115
+ if (names.length >= 2) {
116
+ return (names[0][0] + names[names.length - 1][0]).toUpperCase();
117
+ }
118
+ return names[0][0].toUpperCase();
119
+ }
120
+
121
+ if (user.email) {
122
+ return user.email[0].toUpperCase();
123
+ }
124
+
125
+ return 'U';
126
+ }
127
+
128
+ /**
129
+ * Check if user profile is complete
130
+ */
131
+ export function isProfileComplete(user: User): boolean {
132
+ return !!(
133
+ user.email &&
134
+ user.displayName &&
135
+ user.emailVerified &&
136
+ user.metadata.lastSignInTime
137
+ );
138
+ }
139
+
140
+ /**
141
+ * Get profile completion percentage
142
+ */
143
+ export function getProfileCompletionPercentage(user: User): number {
144
+ let completion = 0;
145
+
146
+ if (user.email) completion += 25;
147
+ if (user.displayName) completion += 25;
148
+ if (user.emailVerified) completion += 25;
149
+ if (user.metadata.lastSignInTime) completion += 25;
150
+
151
+ return completion;
152
+ }
153
+
154
+ /**
155
+ * Check if user can perform sensitive operation
156
+ */
157
+ export function canPerformSensitiveOperation(user: User, maxSessionAgeMs: number = 5 * 60 * 1000): boolean {
158
+ return isSessionValid(user, maxSessionAgeMs);
159
+ }
160
+
161
+ /**
162
+ * Get user account age in days
163
+ */
164
+ export function getAccountAgeDays(user: User): number | null {
165
+ if (!user.metadata.creationTime) {
166
+ return null;
167
+ }
168
+
169
+ const creationTime = new Date(user.metadata.creationTime).getTime();
170
+ const ageMs = Date.now() - creationTime;
171
+
172
+ return Math.floor(ageMs / (24 * 60 * 60 * 1000));
173
+ }
174
+
175
+ /**
176
+ * Check if account is old enough for operation
177
+ */
178
+ export function isAccountOldEnough(user: User, minDays: number = 1): boolean {
179
+ const ageDays = getAccountAgeDays(user);
180
+ return ageDays !== null && ageDays >= minDays;
181
+ }
@@ -0,0 +1,150 @@
1
+ /**
2
+ * User Validation Helpers
3
+ * Single Responsibility: Provide helper methods for user validation
4
+ *
5
+ * Helper methods separated from main UserValidationService.
6
+ * Provides utility functions for user state checking.
7
+ *
8
+ * Max lines: 150 (enforced for maintainability)
9
+ */
10
+
11
+ import type { User } from 'firebase/auth';
12
+ import type { UserValidationService } from './UserValidationService';
13
+
14
+ /**
15
+ * Check if user email matches pattern
16
+ */
17
+ export function isValidEmailFormat(email: string): boolean {
18
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
19
+ return emailRegex.test(email);
20
+ }
21
+
22
+ /**
23
+ * Get password strength indicator
24
+ */
25
+ export function getPasswordStrength(password: string): 'weak' | 'medium' | 'strong' {
26
+ if (password.length < 6) {
27
+ return 'weak';
28
+ }
29
+
30
+ if (password.length < 8) {
31
+ return 'medium';
32
+ }
33
+
34
+ // Check for variety
35
+ const hasLower = /[a-z]/.test(password);
36
+ const hasUpper = /[A-Z]/.test(password);
37
+ const hasNumber = /[0-9]/.test(password);
38
+ const hasSpecial = /[^a-zA-Z0-9]/.test(password);
39
+
40
+ const varietyScore = [hasLower, hasUpper, hasNumber, hasSpecial].filter(Boolean).length;
41
+
42
+ if (varietyScore >= 3) {
43
+ return 'strong';
44
+ }
45
+
46
+ return 'medium';
47
+ }
48
+
49
+ /**
50
+ * Check if account is locked
51
+ */
52
+ export function isAccountLocked(errorCode?: string): boolean {
53
+ return errorCode === 'auth/too-many-requests' || errorCode === 'auth/user-disabled';
54
+ }
55
+
56
+ /**
57
+ * Get user-friendly error message
58
+ */
59
+ export function getAuthErrorMessage(errorCode: string): string {
60
+ const errorMessages: Record<string, string> = {
61
+ 'auth/invalid-credentials': 'Invalid email or password',
62
+ 'auth/user-not-found': 'No account found with this email',
63
+ 'auth/wrong-password': 'Incorrect password',
64
+ 'auth/unverified-email': 'Please verify your email first',
65
+ 'auth/user-disabled': 'This account has been disabled',
66
+ 'auth/too-many-requests': 'Too many attempts. Please try again later',
67
+ 'auth/invalid-email': 'Invalid email address',
68
+ };
69
+
70
+ return errorMessages[errorCode] || 'Authentication failed. Please try again.';
71
+ }
72
+
73
+ /**
74
+ * Check if user session is valid
75
+ */
76
+ export function isSessionValid(user: User, maxAgeMs: number = 30 * 60 * 1000): boolean {
77
+ if (!user.metadata.lastSignInTime) {
78
+ return false;
79
+ }
80
+
81
+ const lastSignIn = new Date(user.metadata.lastSignInTime).getTime();
82
+ const timeSinceSignIn = Date.now() - lastSignIn;
83
+
84
+ return timeSinceSignIn <= maxAgeMs;
85
+ }
86
+
87
+ /**
88
+ * Check if user needs to refresh session
89
+ */
90
+ export function needsSessionRefresh(user: User, maxAgeMs: number = 30 * 60 * 1000): boolean {
91
+ return !isSessionValid(user, maxAgeMs);
92
+ }
93
+
94
+ /**
95
+ * Get user display name
96
+ */
97
+ export function getUserDisplayName(user: User): string {
98
+ if (user.displayName) {
99
+ return user.displayName;
100
+ }
101
+
102
+ if (user.email) {
103
+ return user.email.split('@')[0];
104
+ }
105
+
106
+ return 'User';
107
+ }
108
+
109
+ /**
110
+ * Get user initials for avatar
111
+ */
112
+ export function getUserInitials(user: User): string {
113
+ if (user.displayName) {
114
+ const names = user.displayName.split(' ');
115
+ if (names.length >= 2) {
116
+ return (names[0][0] + names[names.length - 1][0]).toUpperCase();
117
+ }
118
+ return names[0][0].toUpperCase();
119
+ }
120
+
121
+ if (user.email) {
122
+ return user.email[0].toUpperCase();
123
+ }
124
+
125
+ return 'U';
126
+ }
127
+
128
+ /**
129
+ * Check if user profile is complete
130
+ */
131
+ export function isProfileComplete(user: User): boolean {
132
+ return !!(
133
+ user.email &&
134
+ user.displayName &&
135
+ user.emailVerified &&
136
+ user.metadata.lastSignInTime
137
+ );
138
+ }
139
+
140
+ /**
141
+ * Get profile completion percentage
142
+ */
143
+ export function getProfileCompletionPercentage(user: User): number {
144
+ let completion = 0;
145
+
146
+ if (user.email) completion += 25;
147
+ if (user.displayName) completion += 25;
148
+ if (user.emailVerified) completion += 25;
149
+ if (user.metadata.lastSignInTime) completion += 25;
150
+
@@ -0,0 +1,31 @@
1
+ return completion;
2
+ }
3
+
4
+ /**
5
+ * Check if user can perform sensitive operation
6
+ */
7
+ export function canPerformSensitiveOperation(user: User, maxSessionAgeMs: number = 5 * 60 * 1000): boolean {
8
+ return isSessionValid(user, maxSessionAgeMs);
9
+ }
10
+
11
+ /**
12
+ * Get user account age in days
13
+ */
14
+ export function getAccountAgeDays(user: User): number | null {
15
+ if (!user.metadata.creationTime) {
16
+ return null;
17
+ }
18
+
19
+ const creationTime = new Date(user.metadata.creationTime).getTime();
20
+ const ageMs = Date.now() - creationTime;
21
+
22
+ return Math.floor(ageMs / (24 * 60 * 60 * 1000));
23
+ }
24
+
25
+ /**
26
+ * Check if account is old enough for operation
27
+ */
28
+ export function isAccountOldEnough(user: User, minDays: number = 1): boolean {
29
+ const ageDays = getAccountAgeDays(user);
30
+ return ageDays !== null && ageDays >= minDays;
31
+ }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * User Validation Service
2
+ * User Validation Service (Main)
3
3
  * Single Responsibility: Validate user state for account operations
4
4
  *
5
5
  * Domain service that encapsulates user validation logic.
@@ -12,15 +12,6 @@ import type { Auth, User } from 'firebase/auth';
12
12
  import type { Result } from '../../../../shared/domain/utils';
13
13
  import { successResult, failureResultFrom } from '../../../../shared/domain/utils';
14
14
 
15
- /**
16
- * User validation result
17
- */
18
- export interface UserValidationResult {
19
- readonly valid: boolean;
20
- readonly error?: { code: string; message: string };
21
- readonly userId?: string;
22
- }
23
-
24
15
  /**
25
16
  * User validation service
26
17
  * Provides user validation functionality for account operations
@@ -0,0 +1,150 @@
1
+ /**
2
+ * User Validation Service (Main)
3
+ * Single Responsibility: Validate user state for account operations
4
+ *
5
+ * Domain service that encapsulates user validation logic.
6
+ * Moves business logic from infrastructure to domain layer.
7
+ *
8
+ * Max lines: 150 (enforced for maintainability)
9
+ */
10
+
11
+ import type { Auth, User } from 'firebase/auth';
12
+ import type { Result } from '../../../../shared/domain/utils';
13
+ import { successResult, failureResultFrom } from '../../../../shared/domain/utils';
14
+
15
+ /**
16
+ * User validation service
17
+ * Provides user validation functionality for account operations
18
+ */
19
+ export class UserValidationService {
20
+ /**
21
+ * Validate user is ready for deletion
22
+ * Checks user exists, not anonymous, and has required provider
23
+ */
24
+ validateForDeletion(user: User | null): Result<{ userId: string; provider: string }> {
25
+ if (!user) {
26
+ return failureResultFrom('auth/not-ready', 'No authenticated user');
27
+ }
28
+
29
+ if (user.isAnonymous) {
30
+ return failureResultFrom('auth/anonymous', 'Cannot delete anonymous account');
31
+ }
32
+
33
+ const provider = this.getUserAuthProvider(user);
34
+ if (!provider) {
35
+ return failureResultFrom('auth/unsupported', 'Unsupported auth provider');
36
+ }
37
+
38
+ return successResult({
39
+ userId: user.uid,
40
+ provider,
41
+ });
42
+ }
43
+
44
+ /**
45
+ * Validate user hasn't changed during operation
46
+ * Critical for preventing race conditions
47
+ */
48
+ validateUserUnchanged(auth: Auth | null, originalUserId: string): Result<void> {
49
+ if (!auth) {
50
+ return failureResultFrom('auth/not-ready', 'Auth not initialized');
51
+ }
52
+
53
+ const currentUserId = auth.currentUser?.uid;
54
+
55
+ if (!currentUserId) {
56
+ return failureResultFrom('auth/user-signed-out', 'User signed out during operation');
57
+ }
58
+
59
+ if (currentUserId !== originalUserId) {
60
+ return failureResultFrom('auth/user-changed', 'User changed during operation');
61
+ }
62
+
63
+ return successResult();
64
+ }
65
+
66
+ /**
67
+ * Validate user credentials are present
68
+ */
69
+ validateCredentials(
70
+ user: User,
71
+ options: {
72
+ password?: string;
73
+ googleIdToken?: string;
74
+ }
75
+ ): Result<void> {
76
+ const provider = this.getUserAuthProvider(user);
77
+
78
+ if (provider === 'password' && !options.password) {
79
+ return failureResultFrom('auth/password-required', 'Password required for reauthentication');
80
+ }
81
+
82
+ if (provider === 'google.com' && !options.googleIdToken) {
83
+ return failureResultFrom('auth/google-token-required', 'Google ID token required for reauthentication');
84
+ }
85
+
86
+ return successResult();
87
+ }
88
+
89
+ /**
90
+ * Get user's auth provider
91
+ * Returns provider ID or null if unknown
92
+ */
93
+ getUserAuthProvider(user: User): string | null {
94
+ if (!user.providerData || user.providerData.length === 0) {
95
+ return null;
96
+ }
97
+
98
+ // Check all providers
99
+ for (const userInfo of user.providerData) {
100
+ if (userInfo.providerId) {
101
+ return userInfo.providerId;
102
+ }
103
+ }
104
+
105
+ return null;
106
+ }
107
+
108
+ /**
109
+ * Check if user has specific provider
110
+ */
111
+ hasProvider(user: User, providerId: string): boolean {
112
+ if (!user.providerData || user.providerData.length === 0) {
113
+ return false;
114
+ }
115
+
116
+ return user.providerData.some(
117
+ userInfo => userInfo.providerId === providerId
118
+ );
119
+ }
120
+
121
+ /**
122
+ * Check if user is email/password user
123
+ */
124
+ isEmailPasswordUser(user: User): boolean {
125
+ return this.hasProvider(user, 'password');
126
+ }
127
+
128
+ /**
129
+ * Check if user is Google user
130
+ */
131
+ isGoogleUser(user: User): boolean {
132
+ return this.hasProvider(user, 'google.com');
133
+ }
134
+
135
+ /**
136
+ * Check if user is Apple user
137
+ */
138
+ isAppleUser(user: User): boolean {
139
+ return this.hasProvider(user, 'apple.com');
140
+ }
141
+
142
+ /**
143
+ * Check if user requires reauthentication
144
+ * Based on provider and operation type
145
+ */
146
+ requiresReauthentication(user: User, operation: 'delete' | 'update'): boolean {
147
+ // Email/password users always need recent auth
148
+ if (this.isEmailPasswordUser(user)) {
149
+ return true;
150
+ }