@umituz/react-native-firebase 2.5.1 → 2.6.1
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.
- package/package.json +1 -1
- package/src/application/auth/index.ts +42 -0
- package/src/application/auth/ports/AuthPort.ts +164 -0
- package/src/application/auth/use-cases/SignInUseCase.ts +253 -0
- package/src/application/auth/use-cases/SignOutUseCase.ts +288 -0
- package/src/application/auth/use-cases/index.ts +26 -0
- package/src/domains/account-deletion/domain/index.ts +15 -0
- package/src/domains/account-deletion/domain/services/UserValidationService.ts +295 -0
- package/src/domains/account-deletion/index.ts +43 -6
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionExecutor.ts +230 -0
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionReauthHandler.ts +174 -0
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionRepository.ts +266 -0
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionTypes.ts +33 -0
- package/src/domains/account-deletion/infrastructure/services/account-deletion.service.ts +39 -227
- package/src/domains/auth/domain.ts +16 -0
- package/src/domains/auth/index.ts +7 -148
- package/src/domains/auth/infrastructure.ts +156 -0
- package/src/domains/auth/presentation/hooks/GoogleOAuthHookService.ts +247 -0
- package/src/domains/auth/presentation/hooks/useGoogleOAuth.ts +49 -103
- package/src/domains/auth/presentation.ts +25 -0
- package/src/domains/firestore/domain/entities/Collection.ts +288 -0
- package/src/domains/firestore/domain/entities/Document.ts +233 -0
- package/src/domains/firestore/domain/index.ts +30 -0
- package/src/domains/firestore/domain/services/QueryService.ts +182 -0
- package/src/domains/firestore/domain/services/QueryServiceAnalysis.ts +169 -0
- package/src/domains/firestore/domain/services/QueryServiceHelpers.ts +151 -0
- package/src/domains/firestore/domain/value-objects/QueryOptions.ts +191 -0
- package/src/domains/firestore/domain/value-objects/QueryOptions.ts.bak +320 -0
- package/src/domains/firestore/domain/value-objects/QueryOptionsSerialization.ts +207 -0
- package/src/domains/firestore/domain/value-objects/QueryOptionsValidation.ts +182 -0
- package/src/domains/firestore/domain/value-objects/WhereClause.ts +299 -0
- package/src/domains/firestore/domain/value-objects/WhereClauseFactory.ts +207 -0
- package/src/domains/firestore/index.ts +20 -6
- package/src/index.ts +25 -0
- package/src/shared/domain/utils/calculation.util.ts +17 -305
- package/src/shared/domain/utils/error-handlers/error-messages.ts +11 -0
- package/src/shared/domain/utils/index.ts +0 -5
- package/src/shared/infrastructure/base/ErrorHandler.ts +189 -0
- package/src/shared/infrastructure/base/ServiceBase.ts +220 -0
- package/src/shared/infrastructure/base/TypedGuard.ts +131 -0
- package/src/shared/infrastructure/base/index.ts +34 -0
- package/src/shared/infrastructure/config/state/FirebaseClientState.ts +34 -12
- package/src/shared/infrastructure/config/base/ClientStateManager.ts +0 -82
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Account Deletion Executor (Main)
|
|
3
|
+
* Single Responsibility: Execute account deletion with retry logic
|
|
4
|
+
*
|
|
5
|
+
* Infrastructure service that executes account deletion operations.
|
|
6
|
+
* Coordinates reauthentication and deletion with error handling.
|
|
7
|
+
*
|
|
8
|
+
* Max lines: 150 (enforced for maintainability)
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { User } from 'firebase/auth';
|
|
12
|
+
import { getFirebaseAuth } from '../../../auth/infrastructure/config/FirebaseAuthClient';
|
|
13
|
+
import { AccountDeletionRepository } from './AccountDeletionRepository';
|
|
14
|
+
import { userValidationService } from '../../domain/services/UserValidationService';
|
|
15
|
+
import type { Result } from '../../../../shared/domain/utils';
|
|
16
|
+
import type { AccountDeletionOptions } from '../../application/ports/reauthentication.types';
|
|
17
|
+
import type { AccountDeletionResult } from './AccountDeletionTypes';
|
|
18
|
+
import { handleReauthentication } from './AccountDeletionReauthHandler';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Account deletion executor
|
|
22
|
+
* Executes account deletion with automatic reauthentication
|
|
23
|
+
*/
|
|
24
|
+
export class AccountDeletionExecutor {
|
|
25
|
+
private readonly repository: AccountDeletionRepository;
|
|
26
|
+
private deletionInProgress = false;
|
|
27
|
+
|
|
28
|
+
constructor(repository?: AccountDeletionRepository) {
|
|
29
|
+
this.repository = repository || new AccountDeletionRepository();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Delete current user account
|
|
34
|
+
* Handles reauthentication automatically if enabled
|
|
35
|
+
*/
|
|
36
|
+
async deleteCurrentUser(
|
|
37
|
+
options: AccountDeletionOptions = { autoReauthenticate: true }
|
|
38
|
+
): Promise<AccountDeletionResult> {
|
|
39
|
+
// Prevent concurrent deletion attempts
|
|
40
|
+
if (this.deletionInProgress) {
|
|
41
|
+
return {
|
|
42
|
+
success: false,
|
|
43
|
+
error: { code: 'auth/operation-in-progress', message: 'Account deletion already in progress' },
|
|
44
|
+
requiresReauth: false,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
this.deletionInProgress = true;
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
const auth = getFirebaseAuth();
|
|
52
|
+
const user = auth?.currentUser;
|
|
53
|
+
|
|
54
|
+
if (!auth || !user) {
|
|
55
|
+
return {
|
|
56
|
+
success: false,
|
|
57
|
+
error: { code: 'auth/not-ready', message: 'Auth not ready' },
|
|
58
|
+
requiresReauth: false,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const originalUserId = user.uid;
|
|
63
|
+
|
|
64
|
+
// Validate user for deletion
|
|
65
|
+
const validation = await this.repository.validateForDeletion(user);
|
|
66
|
+
if (!validation.success) {
|
|
67
|
+
return {
|
|
68
|
+
success: false,
|
|
69
|
+
error: validation.error,
|
|
70
|
+
requiresReauth: false,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const provider = validation.data!.provider;
|
|
75
|
+
|
|
76
|
+
// Check if reauthentication is needed
|
|
77
|
+
const needsReauth = this.shouldReauthenticate(user, options, provider);
|
|
78
|
+
if (needsReauth) {
|
|
79
|
+
const reauthResult = await handleReauthentication(user, options, originalUserId, this.repository);
|
|
80
|
+
if (reauthResult) {
|
|
81
|
+
return reauthResult;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Attempt deletion
|
|
86
|
+
return await this.performDeletion(user, originalUserId, options);
|
|
87
|
+
} finally {
|
|
88
|
+
this.deletionInProgress = false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Delete specific user account
|
|
94
|
+
* Direct deletion without reauthentication
|
|
95
|
+
*/
|
|
96
|
+
async deleteUserAccount(user: User | null): Promise<AccountDeletionResult> {
|
|
97
|
+
if (!user || user.isAnonymous) {
|
|
98
|
+
return {
|
|
99
|
+
success: false,
|
|
100
|
+
error: { code: 'auth/invalid', message: 'Invalid user' },
|
|
101
|
+
requiresReauth: false,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
try {
|
|
106
|
+
const result = await this.repository.deleteAccount(user);
|
|
107
|
+
if (result.success) {
|
|
108
|
+
return { success: true };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
success: false,
|
|
113
|
+
error: result.error,
|
|
114
|
+
requiresReauth: result.error?.code === 'auth/requires-recent-login',
|
|
115
|
+
};
|
|
116
|
+
} catch (error: unknown) {
|
|
117
|
+
return {
|
|
118
|
+
success: false,
|
|
119
|
+
error: {
|
|
120
|
+
code: 'auth/failed',
|
|
121
|
+
message: error instanceof Error ? error.message : 'Unknown error',
|
|
122
|
+
},
|
|
123
|
+
requiresReauth: false,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Check if reauthentication is needed
|
|
130
|
+
*/
|
|
131
|
+
private shouldReauthenticate(
|
|
132
|
+
user: User,
|
|
133
|
+
options: AccountDeletionOptions,
|
|
134
|
+
provider: string
|
|
135
|
+
): boolean {
|
|
136
|
+
// Password users need reauthentication
|
|
137
|
+
if (provider === 'password' && options.autoReauthenticate && options.onPasswordRequired) {
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Check if credentials are provided
|
|
142
|
+
const hasCredentials = !!(options.password || options.googleIdToken);
|
|
143
|
+
if (hasCredentials) {
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Perform account deletion
|
|
152
|
+
*/
|
|
153
|
+
private async performDeletion(
|
|
154
|
+
user: User,
|
|
155
|
+
originalUserId: string,
|
|
156
|
+
options: AccountDeletionOptions
|
|
157
|
+
): Promise<AccountDeletionResult> {
|
|
158
|
+
try {
|
|
159
|
+
// Validate user hasn't changed
|
|
160
|
+
const auth = getFirebaseAuth();
|
|
161
|
+
const validation = userValidationService.validateUserUnchanged(auth, originalUserId);
|
|
162
|
+
if (!validation.success) {
|
|
163
|
+
return {
|
|
164
|
+
success: false,
|
|
165
|
+
error: validation.error!,
|
|
166
|
+
requiresReauth: false,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Delete account
|
|
171
|
+
const result = await this.repository.deleteAccount(user);
|
|
172
|
+
if (result.success) {
|
|
173
|
+
return { success: true };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Check if reauthentication can help
|
|
177
|
+
const error = result.error;
|
|
178
|
+
if (
|
|
179
|
+
error?.code === 'auth/requires-recent-login' &&
|
|
180
|
+
options.autoReauthenticate
|
|
181
|
+
) {
|
|
182
|
+
const reauthResult = await handleReauthentication(user, options, originalUserId, this.repository);
|
|
183
|
+
if (reauthResult) {
|
|
184
|
+
return reauthResult;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return {
|
|
189
|
+
success: false,
|
|
190
|
+
error,
|
|
191
|
+
requiresReauth: error?.code === 'auth/requires-recent-login',
|
|
192
|
+
};
|
|
193
|
+
} catch (error: unknown) {
|
|
194
|
+
return {
|
|
195
|
+
success: false,
|
|
196
|
+
error: {
|
|
197
|
+
code: 'auth/failed',
|
|
198
|
+
message: error instanceof Error ? error.message : 'Unknown error',
|
|
199
|
+
},
|
|
200
|
+
requiresReauth: false,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Check if deletion is in progress
|
|
207
|
+
*/
|
|
208
|
+
isDeletionInProgress(): boolean {
|
|
209
|
+
return this.deletionInProgress;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Get the repository instance
|
|
214
|
+
*/
|
|
215
|
+
getRepository(): AccountDeletionRepository {
|
|
216
|
+
return this.repository;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Factory function to create account deletion executor
|
|
222
|
+
*/
|
|
223
|
+
export function createAccountDeletionExecutor(): AccountDeletionExecutor {
|
|
224
|
+
return new AccountDeletionExecutor();
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Default singleton instance
|
|
229
|
+
*/
|
|
230
|
+
export const accountDeletionExecutor = createAccountDeletionExecutor();
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Account Deletion Reauthentication Handler
|
|
3
|
+
* Single Responsibility: Handle reauthentication during account deletion
|
|
4
|
+
*
|
|
5
|
+
* Max lines: 150 (enforced for maintainability)
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { User } from 'firebase/auth';
|
|
9
|
+
import { getFirebaseAuth } from '../../../auth/infrastructure/config/FirebaseAuthClient';
|
|
10
|
+
import { userValidationService } from '../../domain/services/UserValidationService';
|
|
11
|
+
import type { Result } from '../../../../shared/domain/utils';
|
|
12
|
+
import type { AccountDeletionOptions } from '../../application/ports/reauthentication.types';
|
|
13
|
+
import type { AccountDeletionResult, ReauthenticationContext } from './AccountDeletionTypes';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Handle reauthentication during account deletion
|
|
17
|
+
* Coordinates reauthentication flow with credential management
|
|
18
|
+
*/
|
|
19
|
+
export async function handleReauthentication(
|
|
20
|
+
user: User,
|
|
21
|
+
options: AccountDeletionOptions,
|
|
22
|
+
originalUserId: string | undefined,
|
|
23
|
+
repository: any
|
|
24
|
+
): Promise<AccountDeletionResult | null> {
|
|
25
|
+
// Validate user hasn't changed before reauthentication
|
|
26
|
+
if (originalUserId) {
|
|
27
|
+
const auth = getFirebaseAuth();
|
|
28
|
+
const validation = userValidationService.validateUserUnchanged(auth, originalUserId);
|
|
29
|
+
if (!validation.success) {
|
|
30
|
+
return {
|
|
31
|
+
success: false,
|
|
32
|
+
error: validation.error!,
|
|
33
|
+
requiresReauth: false,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Import reauthentication functions
|
|
39
|
+
const {
|
|
40
|
+
getUserAuthProvider,
|
|
41
|
+
reauthenticateWithApple,
|
|
42
|
+
reauthenticateWithPassword,
|
|
43
|
+
reauthenticateWithGoogle,
|
|
44
|
+
} = await import('./reauthentication.service');
|
|
45
|
+
|
|
46
|
+
const provider = getUserAuthProvider(user);
|
|
47
|
+
|
|
48
|
+
// Attempt reauthentication based on provider
|
|
49
|
+
let reauthResult: { success: boolean; error?: { code?: string; message?: string } } | null = null;
|
|
50
|
+
|
|
51
|
+
if (provider === 'apple.com') {
|
|
52
|
+
reauthResult = await reauthenticateWithApple(user);
|
|
53
|
+
} else if (provider === 'google.com') {
|
|
54
|
+
const googleToken = await getGoogleToken(options);
|
|
55
|
+
if (!googleToken) {
|
|
56
|
+
return {
|
|
57
|
+
success: false,
|
|
58
|
+
error: { code: 'auth/google-reauth', message: 'Google reauthentication required' },
|
|
59
|
+
requiresReauth: true,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
reauthResult = await reauthenticateWithGoogle(user, googleToken);
|
|
63
|
+
} else if (provider === 'password') {
|
|
64
|
+
const password = await getPassword(options);
|
|
65
|
+
if (!password) {
|
|
66
|
+
return {
|
|
67
|
+
success: false,
|
|
68
|
+
error: { code: 'auth/password-reauth', message: 'Password required' },
|
|
69
|
+
requiresReauth: true,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
reauthResult = await reauthenticateWithPassword(user, password);
|
|
73
|
+
} else {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// If reauthentication successful, retry deletion
|
|
78
|
+
if (reauthResult.success) {
|
|
79
|
+
return await retryDeletionAfterReauth(originalUserId, repository);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
success: false,
|
|
84
|
+
error: {
|
|
85
|
+
code: reauthResult.error?.code || 'auth/reauth-failed',
|
|
86
|
+
message: reauthResult.error?.message || 'Reauthentication failed',
|
|
87
|
+
},
|
|
88
|
+
requiresReauth: true,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Get Google ID token from options or callback
|
|
94
|
+
*/
|
|
95
|
+
async function getGoogleToken(options: AccountDeletionOptions): Promise<string | undefined> {
|
|
96
|
+
if (options.googleIdToken) {
|
|
97
|
+
return options.googleIdToken;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (options.onGoogleReauthRequired) {
|
|
101
|
+
return await options.onGoogleReauthRequired();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Get password from options or callback
|
|
109
|
+
*/
|
|
110
|
+
async function getPassword(options: AccountDeletionOptions): Promise<string | undefined> {
|
|
111
|
+
if (options.password) {
|
|
112
|
+
return options.password;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (options.onPasswordRequired) {
|
|
116
|
+
return await options.onPasswordRequired();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Retry deletion after successful reauthentication
|
|
124
|
+
*/
|
|
125
|
+
async function retryDeletionAfterReauth(
|
|
126
|
+
originalUserId: string | undefined,
|
|
127
|
+
repository: any
|
|
128
|
+
): Promise<AccountDeletionResult> {
|
|
129
|
+
try {
|
|
130
|
+
const auth = getFirebaseAuth();
|
|
131
|
+
const user = auth?.currentUser;
|
|
132
|
+
|
|
133
|
+
if (!user) {
|
|
134
|
+
return {
|
|
135
|
+
success: false,
|
|
136
|
+
error: { code: 'auth/not-ready', message: 'User not found after reauthentication' },
|
|
137
|
+
requiresReauth: false,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Validate user hasn't changed after reauthentication
|
|
142
|
+
if (originalUserId) {
|
|
143
|
+
const validation = userValidationService.validateUserUnchanged(auth, originalUserId);
|
|
144
|
+
if (!validation.success) {
|
|
145
|
+
return {
|
|
146
|
+
success: false,
|
|
147
|
+
error: validation.error!,
|
|
148
|
+
requiresReauth: false,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Delete account
|
|
154
|
+
const result = await repository.deleteAccount(user);
|
|
155
|
+
if (result.success) {
|
|
156
|
+
return { success: true };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
success: false,
|
|
161
|
+
error: result.error,
|
|
162
|
+
requiresReauth: false,
|
|
163
|
+
};
|
|
164
|
+
} catch (error: unknown) {
|
|
165
|
+
return {
|
|
166
|
+
success: false,
|
|
167
|
+
error: {
|
|
168
|
+
code: 'auth/failed',
|
|
169
|
+
message: error instanceof Error ? error.message : 'Unknown error',
|
|
170
|
+
},
|
|
171
|
+
requiresReauth: false,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Account Deletion Repository
|
|
3
|
+
* Single Responsibility: Handle account deletion persistence
|
|
4
|
+
*
|
|
5
|
+
* Infrastructure repository that manages account deletion operations.
|
|
6
|
+
* Uses ServiceBase for error handling and initialization.
|
|
7
|
+
*
|
|
8
|
+
* Max lines: 150 (enforced for maintainability)
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { deleteUser, type User } from 'firebase/auth';
|
|
12
|
+
import { ServiceBase } from '../../../../shared/infrastructure/base/ServiceBase';
|
|
13
|
+
import type { Result } from '../../../../shared/domain/utils';
|
|
14
|
+
import { successResult } from '../../../../shared/domain/utils';
|
|
15
|
+
import { markUserDeleted } from '../../../auth/infrastructure/services/user-document.service';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Account deletion repository
|
|
19
|
+
* Manages account deletion operations and user document cleanup
|
|
20
|
+
*/
|
|
21
|
+
export class AccountDeletionRepository extends ServiceBase {
|
|
22
|
+
constructor() {
|
|
23
|
+
super({
|
|
24
|
+
serviceName: 'AccountDeletionRepository',
|
|
25
|
+
autoInitialize: true,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Delete user account from Firebase Auth
|
|
31
|
+
* Marks user document as deleted before account removal
|
|
32
|
+
*/
|
|
33
|
+
async deleteAccount(user: User): Promise<Result<void>> {
|
|
34
|
+
return this.execute(async () => {
|
|
35
|
+
this.log('Deleting account', { userId: user.uid });
|
|
36
|
+
|
|
37
|
+
// Mark user document as deleted
|
|
38
|
+
const marked = await markUserDeleted(user.uid);
|
|
39
|
+
if (!marked && __DEV__) {
|
|
40
|
+
this.logError('Failed to mark user document as deleted');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Delete user account
|
|
44
|
+
await deleteUser(user);
|
|
45
|
+
|
|
46
|
+
this.log('Account deleted successfully', { userId: user.uid });
|
|
47
|
+
}, 'account-deletion/delete-failed');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Validate user can be deleted
|
|
52
|
+
* Checks user is not anonymous and has valid provider
|
|
53
|
+
*/
|
|
54
|
+
async validateForDeletion(user: User | null): Promise<Result<{ userId: string; provider: string }>> {
|
|
55
|
+
return this.executeSync(() => {
|
|
56
|
+
if (!user) {
|
|
57
|
+
return {
|
|
58
|
+
success: false,
|
|
59
|
+
error: {
|
|
60
|
+
code: 'auth/not-ready',
|
|
61
|
+
message: 'No authenticated user',
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (user.isAnonymous) {
|
|
67
|
+
return {
|
|
68
|
+
success: false,
|
|
69
|
+
error: {
|
|
70
|
+
code: 'auth/anonymous',
|
|
71
|
+
message: 'Cannot delete anonymous account',
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const provider = this.getUserAuthProvider(user);
|
|
77
|
+
if (!provider) {
|
|
78
|
+
return {
|
|
79
|
+
success: false,
|
|
80
|
+
error: {
|
|
81
|
+
code: 'auth/unsupported',
|
|
82
|
+
message: 'Unsupported auth provider',
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return successResult({
|
|
88
|
+
userId: user.uid,
|
|
89
|
+
provider,
|
|
90
|
+
});
|
|
91
|
+
}, 'account-deletion/validation-failed');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Get user's auth provider
|
|
96
|
+
*/
|
|
97
|
+
private getUserAuthProvider(user: User): string | null {
|
|
98
|
+
if (!user.providerData || user.providerData.length === 0) {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
for (const userInfo of user.providerData) {
|
|
103
|
+
if (userInfo.providerId) {
|
|
104
|
+
return userInfo.providerId;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Check if user is email/password user
|
|
113
|
+
*/
|
|
114
|
+
isEmailPasswordUser(user: User): boolean {
|
|
115
|
+
return this.getUserAuthProvider(user) === 'password';
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Check if user is Google user
|
|
120
|
+
*/
|
|
121
|
+
isGoogleUser(user: User): boolean {
|
|
122
|
+
return this.getUserAuthProvider(user) === 'google.com';
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Check if user is Apple user
|
|
127
|
+
*/
|
|
128
|
+
isAppleUser(user: User): boolean {
|
|
129
|
+
return this.getUserAuthProvider(user) === 'apple.com';
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Get user ID from user object
|
|
134
|
+
*/
|
|
135
|
+
getUserId(user: User): string {
|
|
136
|
+
return user.uid;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Check if user email is verified
|
|
141
|
+
*/
|
|
142
|
+
isEmailVerified(user: User): boolean {
|
|
143
|
+
return user.emailVerified || false;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Get user email
|
|
148
|
+
*/
|
|
149
|
+
getEmail(user: User): string | null {
|
|
150
|
+
return user.email || null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Get account creation time
|
|
155
|
+
*/
|
|
156
|
+
getCreationTime(user: User): Date | null {
|
|
157
|
+
if (!user.metadata.creationTime) {
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
return new Date(user.metadata.creationTime);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Get last sign-in time
|
|
165
|
+
*/
|
|
166
|
+
getLastSignInTime(user: User): Date | null {
|
|
167
|
+
if (!user.metadata.lastSignInTime) {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
return new Date(user.metadata.lastSignInTime);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Check if account is new (created within specified days)
|
|
175
|
+
*/
|
|
176
|
+
isAccountNew(user: User, maxAgeDays: number = 1): boolean {
|
|
177
|
+
const creationTime = this.getCreationTime(user);
|
|
178
|
+
if (!creationTime) return false;
|
|
179
|
+
|
|
180
|
+
const ageMs = Date.now() - creationTime.getTime();
|
|
181
|
+
const maxAgeMs = maxAgeDays * 24 * 60 * 60 * 1000;
|
|
182
|
+
|
|
183
|
+
return ageMs <= maxAgeMs;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Check if user recently signed in
|
|
188
|
+
*/
|
|
189
|
+
isRecentSignIn(user: User, maxAgeMinutes: number = 5): boolean {
|
|
190
|
+
const lastSignIn = this.getLastSignInTime(user);
|
|
191
|
+
if (!lastSignIn) return false;
|
|
192
|
+
|
|
193
|
+
const timeSinceSignIn = Date.now() - lastSignIn.getTime();
|
|
194
|
+
const maxAgeMs = maxAgeMinutes * 60 * 1000;
|
|
195
|
+
|
|
196
|
+
return timeSinceSignIn <= maxAgeMs;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Mark user as deleted in database
|
|
201
|
+
* Separate method for flexibility
|
|
202
|
+
*/
|
|
203
|
+
async markUserDeleted(userId: string): Promise<Result<void>> {
|
|
204
|
+
return this.execute(async () => {
|
|
205
|
+
this.log('Marking user as deleted', { userId });
|
|
206
|
+
|
|
207
|
+
const marked = await markUserDeleted(userId);
|
|
208
|
+
if (!marked) {
|
|
209
|
+
this.logError('Failed to mark user as deleted in database');
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return successResult();
|
|
213
|
+
}, 'account-deletion/mark-failed');
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Cleanup user data
|
|
218
|
+
* Override in subclass for custom cleanup logic
|
|
219
|
+
*/
|
|
220
|
+
protected async cleanupUserData(userId: string): Promise<Result<void>> {
|
|
221
|
+
return this.execute(async () => {
|
|
222
|
+
this.log('Cleaning up user data', { userId });
|
|
223
|
+
// Override in subclass for custom cleanup
|
|
224
|
+
return successResult();
|
|
225
|
+
}, 'account-deletion/cleanup-failed');
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Complete deletion with cleanup
|
|
230
|
+
* Deletes account and cleans up user data
|
|
231
|
+
*/
|
|
232
|
+
async deleteWithCleanup(user: User): Promise<Result<void>> {
|
|
233
|
+
return this.execute(async () => {
|
|
234
|
+
this.log('Starting deletion with cleanup', { userId: user.uid });
|
|
235
|
+
|
|
236
|
+
// Delete account (includes marking document as deleted)
|
|
237
|
+
const deleteResult = await this.deleteAccount(user);
|
|
238
|
+
if (!deleteResult.success) {
|
|
239
|
+
return deleteResult;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Cleanup additional user data
|
|
243
|
+
const cleanupResult = await this.cleanupUserData(user.uid);
|
|
244
|
+
if (!cleanupResult.success) {
|
|
245
|
+
this.logError('Cleanup failed, but account was deleted', {
|
|
246
|
+
userId: user.uid,
|
|
247
|
+
error: cleanupResult.error,
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
this.log('Deletion with cleanup completed', { userId: user.uid });
|
|
252
|
+
}, 'account-deletion/complete-failed');
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Factory function to create account deletion repository
|
|
258
|
+
*/
|
|
259
|
+
export function createAccountDeletionRepository(): AccountDeletionRepository {
|
|
260
|
+
return new AccountDeletionRepository();
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Default singleton instance
|
|
265
|
+
*/
|
|
266
|
+
export const accountDeletionRepository = createAccountDeletionRepository();
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Account Deletion Types
|
|
3
|
+
* Single Responsibility: Define account deletion types
|
|
4
|
+
*
|
|
5
|
+
* Max lines: 150 (enforced for maintainability)
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { Result } from '../../../../shared/domain/utils';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Account deletion result
|
|
12
|
+
*/
|
|
13
|
+
export interface AccountDeletionResult extends Result<void> {
|
|
14
|
+
readonly requiresReauth?: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Reauthentication context
|
|
19
|
+
*/
|
|
20
|
+
export interface ReauthenticationContext {
|
|
21
|
+
readonly user: import('firebase/auth').User;
|
|
22
|
+
readonly options: import('../../application/ports/reauthentication.types').AccountDeletionOptions;
|
|
23
|
+
readonly originalUserId?: string;
|
|
24
|
+
readonly repository: AccountDeletionRepository;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Reauthentication result
|
|
29
|
+
*/
|
|
30
|
+
export interface ReauthenticationResult {
|
|
31
|
+
readonly success: boolean;
|
|
32
|
+
readonly error?: { code?: string; message?: string };
|
|
33
|
+
}
|