@umituz/react-native-auth 1.1.1 → 1.2.0
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
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
* Validates and stores authentication configuration
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
import type { User } from "firebase/auth";
|
|
7
|
+
|
|
6
8
|
export interface AuthConfig {
|
|
7
9
|
/** Minimum password length (default: 6) */
|
|
8
10
|
minPasswordLength?: number;
|
|
@@ -15,18 +17,16 @@ export interface AuthConfig {
|
|
|
15
17
|
/** Require special characters in password */
|
|
16
18
|
requireSpecialChars?: boolean;
|
|
17
19
|
/** Callback for user profile creation after signup */
|
|
18
|
-
onUserCreated?: (user:
|
|
20
|
+
onUserCreated?: (user: User, username?: string) => Promise<void> | void;
|
|
19
21
|
/** Callback for user profile update */
|
|
20
|
-
onUserUpdated?: (user:
|
|
21
|
-
/** Callback after successful sign in */
|
|
22
|
-
onSignIn?: (user: any) => Promise<void> | void;
|
|
22
|
+
onUserUpdated?: (user: User) => Promise<void> | void;
|
|
23
23
|
/** Callback for sign out cleanup */
|
|
24
24
|
onSignOut?: () => Promise<void> | void;
|
|
25
|
-
/** Callback
|
|
26
|
-
|
|
25
|
+
/** Callback for account deletion (optional, for app-specific cleanup) */
|
|
26
|
+
onAccountDeleted?: (userId: string) => Promise<void> | void;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
export const DEFAULT_AUTH_CONFIG: Required<Omit<AuthConfig, 'onUserCreated' | 'onUserUpdated' | 'onSignOut'>> = {
|
|
29
|
+
export const DEFAULT_AUTH_CONFIG: Required<Omit<AuthConfig, 'onUserCreated' | 'onUserUpdated' | 'onSignOut' | 'onAccountDeleted'>> = {
|
|
30
30
|
minPasswordLength: 6,
|
|
31
31
|
requireUppercase: false,
|
|
32
32
|
requireLowercase: false,
|
|
@@ -41,7 +41,7 @@ function validateEmail(email: string): boolean {
|
|
|
41
41
|
*/
|
|
42
42
|
function validatePassword(
|
|
43
43
|
password: string,
|
|
44
|
-
config: Required<Omit<AuthConfig, "onUserCreated" | "onUserUpdated" | "onSignOut">>
|
|
44
|
+
config: Required<Omit<AuthConfig, "onUserCreated" | "onUserUpdated" | "onSignOut" | "onAccountDeleted">>
|
|
45
45
|
): { valid: boolean; error?: string } {
|
|
46
46
|
if (password.length < config.minPasswordLength) {
|
|
47
47
|
return {
|
|
@@ -83,10 +83,13 @@ function validatePassword(
|
|
|
83
83
|
|
|
84
84
|
/**
|
|
85
85
|
* Map Firebase Auth errors to domain errors
|
|
86
|
+
* Type-safe error mapping
|
|
86
87
|
*/
|
|
87
|
-
function mapFirebaseAuthError(error:
|
|
88
|
-
|
|
89
|
-
const
|
|
88
|
+
function mapFirebaseAuthError(error: unknown): Error {
|
|
89
|
+
// Type guard for Firebase Auth errors
|
|
90
|
+
const firebaseError = error as { code?: string; message?: string };
|
|
91
|
+
const code = firebaseError?.code || "";
|
|
92
|
+
const message = firebaseError?.message || "Authentication failed";
|
|
90
93
|
|
|
91
94
|
// Firebase Auth error codes
|
|
92
95
|
if (code === "auth/email-already-in-use") {
|
|
@@ -151,7 +154,7 @@ export class AuthService implements IAuthService {
|
|
|
151
154
|
private getAuth(): Auth | null {
|
|
152
155
|
if (!this.auth) {
|
|
153
156
|
/* eslint-disable-next-line no-console */
|
|
154
|
-
if (__DEV__) {
|
|
157
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
155
158
|
console.warn("Auth service is not initialized. Call initialize() first.");
|
|
156
159
|
}
|
|
157
160
|
return null;
|
|
@@ -174,7 +177,10 @@ export class AuthService implements IAuthService {
|
|
|
174
177
|
}
|
|
175
178
|
|
|
176
179
|
// Validate password
|
|
177
|
-
const passwordValidation = validatePassword(
|
|
180
|
+
const passwordValidation = validatePassword(
|
|
181
|
+
params.password,
|
|
182
|
+
this.config as Required<Omit<AuthConfig, "onUserCreated" | "onUserUpdated" | "onSignOut" | "onAccountDeleted">>
|
|
183
|
+
);
|
|
178
184
|
if (!passwordValidation.valid) {
|
|
179
185
|
throw new AuthWeakPasswordError(passwordValidation.error);
|
|
180
186
|
}
|
|
@@ -202,17 +208,14 @@ export class AuthService implements IAuthService {
|
|
|
202
208
|
// Call user created callback if provided
|
|
203
209
|
if (this.config.onUserCreated) {
|
|
204
210
|
try {
|
|
205
|
-
await this.config.onUserCreated(userCredential.user,
|
|
206
|
-
username: params.username,
|
|
207
|
-
displayName: params.displayName,
|
|
208
|
-
});
|
|
211
|
+
await this.config.onUserCreated(userCredential.user, params.username);
|
|
209
212
|
} catch (callbackError) {
|
|
210
213
|
// Don't fail signup if callback fails
|
|
211
214
|
}
|
|
212
215
|
}
|
|
213
216
|
|
|
214
217
|
return userCredential.user;
|
|
215
|
-
} catch (error:
|
|
218
|
+
} catch (error: unknown) {
|
|
216
219
|
throw mapFirebaseAuthError(error);
|
|
217
220
|
}
|
|
218
221
|
}
|
|
@@ -244,18 +247,8 @@ export class AuthService implements IAuthService {
|
|
|
244
247
|
);
|
|
245
248
|
|
|
246
249
|
this.isGuestMode = false;
|
|
247
|
-
|
|
248
|
-
// Call sign in callback if provided
|
|
249
|
-
if (this.config.onSignIn) {
|
|
250
|
-
try {
|
|
251
|
-
await this.config.onSignIn(userCredential.user);
|
|
252
|
-
} catch (callbackError) {
|
|
253
|
-
// Don't fail signin if callback fails
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
250
|
return userCredential.user;
|
|
258
|
-
} catch (error:
|
|
251
|
+
} catch (error: unknown) {
|
|
259
252
|
throw mapFirebaseAuthError(error);
|
|
260
253
|
}
|
|
261
254
|
}
|
|
@@ -283,7 +276,7 @@ export class AuthService implements IAuthService {
|
|
|
283
276
|
// Don't fail signout if callback fails
|
|
284
277
|
}
|
|
285
278
|
}
|
|
286
|
-
} catch (error:
|
|
279
|
+
} catch (error: unknown) {
|
|
287
280
|
throw mapFirebaseAuthError(error);
|
|
288
281
|
}
|
|
289
282
|
}
|
|
@@ -304,15 +297,6 @@ export class AuthService implements IAuthService {
|
|
|
304
297
|
}
|
|
305
298
|
|
|
306
299
|
this.isGuestMode = true;
|
|
307
|
-
|
|
308
|
-
// Call guest mode enabled callback if provided
|
|
309
|
-
if (this.config.onGuestModeEnabled) {
|
|
310
|
-
try {
|
|
311
|
-
await this.config.onGuestModeEnabled();
|
|
312
|
-
} catch (callbackError) {
|
|
313
|
-
// Don't fail guest mode if callback fails
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
300
|
}
|
|
317
301
|
|
|
318
302
|
/**
|
|
@@ -382,7 +366,7 @@ export function initializeAuthService(
|
|
|
382
366
|
export function getAuthService(): AuthService | null {
|
|
383
367
|
if (!authServiceInstance || !authServiceInstance.isInitialized()) {
|
|
384
368
|
/* eslint-disable-next-line no-console */
|
|
385
|
-
if (__DEV__) {
|
|
369
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
386
370
|
console.warn(
|
|
387
371
|
"Auth service is not initialized. Call initializeAuthService() first."
|
|
388
372
|
);
|