@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.
- package/package.json +1 -1
- package/src/application/auth/ports/AuthPort_part_aa +150 -0
- package/src/application/auth/ports/AuthPort_part_ab +14 -0
- package/src/application/auth/use-cases/SignInUseCaseHelpers.ts +0 -0
- package/src/application/auth/use-cases/SignInUseCaseMain.ts +0 -0
- package/src/application/auth/use-cases/SignInUseCase_part_aa +150 -0
- package/src/application/auth/use-cases/SignInUseCase_part_ab +103 -0
- package/src/application/auth/use-cases/SignOutUseCaseCleanup.ts +0 -0
- package/src/application/auth/use-cases/SignOutUseCaseMain.ts +0 -0
- package/src/application/auth/use-cases/SignOutUseCase_part_aa +150 -0
- package/src/application/auth/use-cases/SignOutUseCase_part_ab +138 -0
- package/src/domains/account-deletion/domain/services/UserValidationHelpers.ts.bak +181 -0
- package/src/domains/account-deletion/domain/services/UserValidationHelpers_part_aa +150 -0
- package/src/domains/account-deletion/domain/services/UserValidationHelpers_part_ab +31 -0
- package/src/domains/account-deletion/domain/services/{UserValidationService.ts → UserValidationService.ts.bak} +1 -10
- package/src/domains/account-deletion/domain/services/UserValidationService_part_aa +150 -0
- package/src/domains/account-deletion/domain/services/UserValidationService_part_ab +136 -0
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionExecutor_part_aa +150 -0
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionExecutor_part_ab +80 -0
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionReauthHandler_part_aa +150 -0
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionReauthHandler_part_ab +24 -0
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionRepository_part_aa +150 -0
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionRepository_part_ab +116 -0
- package/src/domains/account-deletion/infrastructure/services/reauthentication.service_part_aa +150 -0
- package/src/domains/account-deletion/infrastructure/services/reauthentication.service_part_ab +10 -0
- package/src/domains/auth/infrastructure_part_aa +150 -0
- package/src/domains/auth/infrastructure_part_ab +6 -0
- package/src/domains/auth/presentation/hooks/GoogleOAuthHelpers.ts +0 -0
- package/src/domains/auth/presentation/hooks/GoogleOAuthHookService_part_aa +150 -0
- package/src/domains/auth/presentation/hooks/GoogleOAuthHookService_part_ab +97 -0
- package/src/domains/auth/presentation/hooks/GoogleOAuthService.ts +0 -0
- package/src/domains/firestore/domain/entities/Collection.ts +31 -191
- package/src/domains/firestore/domain/entities/Collection.ts.bak +288 -0
- package/src/domains/firestore/domain/entities/CollectionFactory.ts +55 -0
- package/src/domains/firestore/domain/entities/CollectionHelpers.ts +143 -0
- package/src/domains/firestore/domain/entities/CollectionUtils.ts +72 -0
- package/src/domains/firestore/domain/entities/CollectionValidation.ts +138 -0
- package/src/domains/firestore/domain/entities/Collection_part_aa +150 -0
- package/src/domains/firestore/domain/entities/Collection_part_ab +138 -0
- package/src/domains/firestore/domain/entities/DocumentHelpers.ts +0 -0
- package/src/domains/firestore/domain/entities/DocumentMain.ts +0 -0
- package/src/domains/firestore/domain/entities/Document_part_aa +150 -0
- package/src/domains/firestore/domain/entities/Document_part_ab +83 -0
- package/src/domains/firestore/domain/index.ts +44 -9
- package/src/domains/firestore/domain/services/QueryServiceAnalysis_part_aa +150 -0
- package/src/domains/firestore/domain/services/QueryServiceAnalysis_part_ab +19 -0
- package/src/domains/firestore/domain/services/QueryServiceHelpers_part_aa +150 -0
- package/src/domains/firestore/domain/services/QueryServiceHelpers_part_ab +1 -0
- package/src/domains/firestore/domain/services/QueryService_part_aa +150 -0
- package/src/domains/firestore/domain/services/QueryService_part_ab +32 -0
- package/src/domains/firestore/domain/value-objects/QueryOptions.ts.bak +6 -135
- package/src/domains/firestore/domain/value-objects/QueryOptionsSerialization_part_aa +150 -0
- package/src/domains/firestore/domain/value-objects/QueryOptionsSerialization_part_ab +57 -0
- package/src/domains/firestore/domain/value-objects/QueryOptionsValidation_part_aa +150 -0
- package/src/domains/firestore/domain/value-objects/QueryOptionsValidation_part_ab +32 -0
- package/src/domains/firestore/domain/value-objects/{QueryOptions.ts → QueryOptions_part_aa} +0 -41
- package/src/domains/firestore/domain/value-objects/QueryOptions_part_ab +41 -0
- package/src/domains/firestore/domain/value-objects/WhereClauseFactory_part_aa +150 -0
- package/src/domains/firestore/domain/value-objects/WhereClauseFactory_part_ab +57 -0
- package/src/domains/firestore/domain/value-objects/WhereClause_part_aa +150 -0
- package/src/domains/firestore/domain/value-objects/WhereClause_part_ab +149 -0
- package/src/shared/infrastructure/base/ErrorHandler_part_aa +150 -0
- package/src/shared/infrastructure/base/ErrorHandler_part_ab +39 -0
- package/src/shared/infrastructure/base/ServiceBase_part_aa +150 -0
- package/src/shared/infrastructure/base/ServiceBase_part_ab +70 -0
- package/src/shared/infrastructure/config/base/ServiceClientSingleton_part_aa +150 -0
- package/src/shared/infrastructure/config/base/ServiceClientSingleton_part_ab +5 -0
- /package/src/application/auth/ports/{AuthPort.ts → AuthPort.ts.bak} +0 -0
- /package/src/application/auth/use-cases/{SignInUseCase.ts → SignInUseCase.ts.bak} +0 -0
- /package/src/application/auth/use-cases/{SignOutUseCase.ts → SignOutUseCase.ts.bak} +0 -0
- /package/src/domains/account-deletion/infrastructure/services/{AccountDeletionExecutor.ts → AccountDeletionExecutor.ts.bak} +0 -0
- /package/src/domains/account-deletion/infrastructure/services/{AccountDeletionReauthHandler.ts → AccountDeletionReauthHandler.ts.bak} +0 -0
- /package/src/domains/account-deletion/infrastructure/services/{AccountDeletionRepository.ts → AccountDeletionRepository.ts.bak} +0 -0
- /package/src/domains/account-deletion/infrastructure/services/{reauthentication.service.ts → reauthentication.service.ts.bak} +0 -0
- /package/src/domains/auth/{infrastructure.ts → infrastructure.ts.bak} +0 -0
- /package/src/domains/auth/presentation/hooks/{GoogleOAuthHookService.ts → GoogleOAuthHookService.ts.bak} +0 -0
- /package/src/domains/firestore/domain/entities/{Document.ts → Document.ts.bak} +0 -0
- /package/src/domains/firestore/domain/services/{QueryService.ts → QueryService.ts.bak} +0 -0
- /package/src/domains/firestore/domain/services/{QueryServiceAnalysis.ts → QueryServiceAnalysis.ts.bak} +0 -0
- /package/src/domains/firestore/domain/services/{QueryServiceHelpers.ts → QueryServiceHelpers.ts.bak} +0 -0
- /package/src/domains/firestore/domain/value-objects/{QueryOptionsSerialization.ts → QueryOptionsSerialization.ts.bak} +0 -0
- /package/src/domains/firestore/domain/value-objects/{QueryOptionsValidation.ts → QueryOptionsValidation.ts.bak} +0 -0
- /package/src/domains/firestore/domain/value-objects/{WhereClause.ts → WhereClause.ts.bak} +0 -0
- /package/src/domains/firestore/domain/value-objects/{WhereClauseFactory.ts → WhereClauseFactory.ts.bak} +0 -0
- /package/src/shared/infrastructure/base/{ErrorHandler.ts → ErrorHandler.ts.bak} +0 -0
- /package/src/shared/infrastructure/base/{ServiceBase.ts → ServiceBase.ts.bak} +0 -0
- /package/src/shared/infrastructure/config/base/{ServiceClientSingleton.ts → ServiceClientSingleton.ts.bak} +0 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
|
|
2
|
+
// Social providers may require recent auth for sensitive operations
|
|
3
|
+
if (operation === 'delete') {
|
|
4
|
+
return true;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Validate user email
|
|
12
|
+
*/
|
|
13
|
+
validateEmail(user: User): Result<void> {
|
|
14
|
+
const email = user.email;
|
|
15
|
+
|
|
16
|
+
if (!email) {
|
|
17
|
+
return failureResultFrom('auth/no-email', 'User has no email');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
21
|
+
if (!emailRegex.test(email)) {
|
|
22
|
+
return failureResultFrom('auth/invalid-email', 'Invalid email format');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return successResult();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Validate user is verified (if applicable)
|
|
30
|
+
*/
|
|
31
|
+
validateVerified(user: User, requireVerification: boolean = false): Result<void> {
|
|
32
|
+
if (requireVerification && !user.emailVerified) {
|
|
33
|
+
return failureResultFrom('auth/unverified', 'Email not verified');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return successResult();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Get user metadata
|
|
41
|
+
*/
|
|
42
|
+
getUserMetadata(user: User): {
|
|
43
|
+
readonly createdAt: number | null;
|
|
44
|
+
readonly lastSignInAt: number | null;
|
|
45
|
+
} {
|
|
46
|
+
return {
|
|
47
|
+
createdAt: user.metadata.creationTime ? new Date(user.metadata.creationTime).getTime() : null,
|
|
48
|
+
lastSignInAt: user.metadata.lastSignInTime ? new Date(user.metadata.lastSignInTime).getTime() : null,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Check if account is new (created recently)
|
|
54
|
+
*/
|
|
55
|
+
isAccountNew(user: User, maxAgeMs: number = 24 * 60 * 60 * 1000): boolean {
|
|
56
|
+
const metadata = this.getUserMetadata(user);
|
|
57
|
+
if (!metadata.createdAt) return false;
|
|
58
|
+
|
|
59
|
+
const age = Date.now() - metadata.createdAt;
|
|
60
|
+
return age <= maxAgeMs;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Check if user recently signed in
|
|
65
|
+
*/
|
|
66
|
+
isRecentSignIn(user: User, maxAgeMs: number = 5 * 60 * 1000): boolean {
|
|
67
|
+
const metadata = this.getUserMetadata(user);
|
|
68
|
+
if (!metadata.lastSignInAt) return false;
|
|
69
|
+
|
|
70
|
+
const timeSinceSignIn = Date.now() - metadata.lastSignInAt;
|
|
71
|
+
return timeSinceSignIn <= maxAgeMs;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Validate user can perform operation
|
|
76
|
+
* Comprehensive check combining multiple validations
|
|
77
|
+
*/
|
|
78
|
+
validateCanPerformOperation(
|
|
79
|
+
user: User | null,
|
|
80
|
+
operation: 'delete' | 'update',
|
|
81
|
+
options: {
|
|
82
|
+
requireVerified?: boolean;
|
|
83
|
+
maxSignInAge?: number;
|
|
84
|
+
password?: string;
|
|
85
|
+
googleIdToken?: string;
|
|
86
|
+
} = {}
|
|
87
|
+
): Result<{ userId: string; provider: string }> {
|
|
88
|
+
// Validate user ready for operation
|
|
89
|
+
const deletionValidation = this.validateForDeletion(user);
|
|
90
|
+
if (!deletionValidation.success) {
|
|
91
|
+
return deletionValidation;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const userId = deletionValidation.data!.userId;
|
|
95
|
+
const provider = deletionValidation.data!.provider;
|
|
96
|
+
|
|
97
|
+
// Validate email
|
|
98
|
+
if (user) {
|
|
99
|
+
const emailValidation = this.validateEmail(user);
|
|
100
|
+
if (!emailValidation.success) {
|
|
101
|
+
return emailValidation;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Validate verification status
|
|
105
|
+
const verifiedValidation = this.validateVerified(user, options.requireVerified);
|
|
106
|
+
if (!verifiedValidation.success) {
|
|
107
|
+
return verifiedValidation;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Check if recent sign-in required
|
|
111
|
+
if (options.maxSignInAge && !this.isRecentSignIn(user, options.maxSignInAge)) {
|
|
112
|
+
return failureResultFrom('auth/stale-session', 'Session too old, please sign in again');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Validate credentials
|
|
116
|
+
const credentialsValidation = this.validateCredentials(user, options);
|
|
117
|
+
if (!credentialsValidation.success) {
|
|
118
|
+
return credentialsValidation;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return successResult({ userId, provider });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Factory function to create user validation service
|
|
128
|
+
*/
|
|
129
|
+
export function createUserValidationService(): UserValidationService {
|
|
130
|
+
return new UserValidationService();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Default instance for convenience
|
|
135
|
+
*/
|
|
136
|
+
export const userValidationService = createUserValidationService();
|
|
@@ -0,0 +1,150 @@
|
|
|
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
|
+
/**
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
* Perform account deletion
|
|
2
|
+
*/
|
|
3
|
+
private async performDeletion(
|
|
4
|
+
user: User,
|
|
5
|
+
originalUserId: string,
|
|
6
|
+
options: AccountDeletionOptions
|
|
7
|
+
): Promise<AccountDeletionResult> {
|
|
8
|
+
try {
|
|
9
|
+
// Validate user hasn't changed
|
|
10
|
+
const auth = getFirebaseAuth();
|
|
11
|
+
const validation = userValidationService.validateUserUnchanged(auth, originalUserId);
|
|
12
|
+
if (!validation.success) {
|
|
13
|
+
return {
|
|
14
|
+
success: false,
|
|
15
|
+
error: validation.error!,
|
|
16
|
+
requiresReauth: false,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Delete account
|
|
21
|
+
const result = await this.repository.deleteAccount(user);
|
|
22
|
+
if (result.success) {
|
|
23
|
+
return { success: true };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Check if reauthentication can help
|
|
27
|
+
const error = result.error;
|
|
28
|
+
if (
|
|
29
|
+
error?.code === 'auth/requires-recent-login' &&
|
|
30
|
+
options.autoReauthenticate
|
|
31
|
+
) {
|
|
32
|
+
const reauthResult = await handleReauthentication(user, options, originalUserId, this.repository);
|
|
33
|
+
if (reauthResult) {
|
|
34
|
+
return reauthResult;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
success: false,
|
|
40
|
+
error,
|
|
41
|
+
requiresReauth: error?.code === 'auth/requires-recent-login',
|
|
42
|
+
};
|
|
43
|
+
} catch (error: unknown) {
|
|
44
|
+
return {
|
|
45
|
+
success: false,
|
|
46
|
+
error: {
|
|
47
|
+
code: 'auth/failed',
|
|
48
|
+
message: error instanceof Error ? error.message : 'Unknown error',
|
|
49
|
+
},
|
|
50
|
+
requiresReauth: false,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Check if deletion is in progress
|
|
57
|
+
*/
|
|
58
|
+
isDeletionInProgress(): boolean {
|
|
59
|
+
return this.deletionInProgress;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Get the repository instance
|
|
64
|
+
*/
|
|
65
|
+
getRepository(): AccountDeletionRepository {
|
|
66
|
+
return this.repository;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Factory function to create account deletion executor
|
|
72
|
+
*/
|
|
73
|
+
export function createAccountDeletionExecutor(): AccountDeletionExecutor {
|
|
74
|
+
return new AccountDeletionExecutor();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Default singleton instance
|
|
79
|
+
*/
|
|
80
|
+
export const accountDeletionExecutor = createAccountDeletionExecutor();
|
package/src/domains/account-deletion/infrastructure/services/AccountDeletionReauthHandler_part_aa
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
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
|
+
}
|
package/src/domains/account-deletion/infrastructure/services/AccountDeletionReauthHandler_part_ab
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
}
|
|
2
|
+
|
|
3
|
+
// Delete account
|
|
4
|
+
const result = await repository.deleteAccount(user);
|
|
5
|
+
if (result.success) {
|
|
6
|
+
return { success: true };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
return {
|
|
10
|
+
success: false,
|
|
11
|
+
error: result.error,
|
|
12
|
+
requiresReauth: false,
|
|
13
|
+
};
|
|
14
|
+
} catch (error: unknown) {
|
|
15
|
+
return {
|
|
16
|
+
success: false,
|
|
17
|
+
error: {
|
|
18
|
+
code: 'auth/failed',
|
|
19
|
+
message: error instanceof Error ? error.message : 'Unknown error',
|
|
20
|
+
},
|
|
21
|
+
requiresReauth: false,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/domains/account-deletion/infrastructure/services/AccountDeletionRepository_part_aa
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
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;
|