@umituz/react-native-auth 1.1.2 → 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
|
@@ -83,11 +83,3 @@ export class AuthInvalidEmailError extends AuthError {
|
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
export class AuthInvalidCredentialError extends AuthError {
|
|
87
|
-
constructor(message: string = "Invalid email or password") {
|
|
88
|
-
super(message, "AUTH_INVALID_CREDENTIAL");
|
|
89
|
-
this.name = "AuthInvalidCredentialError";
|
|
90
|
-
Object.setPrototypeOf(this, AuthInvalidCredentialError.prototype);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
@@ -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,
|
package/src/index.ts
CHANGED
|
@@ -24,7 +24,6 @@ import {
|
|
|
24
24
|
AuthWrongPasswordError,
|
|
25
25
|
AuthUserNotFoundError,
|
|
26
26
|
AuthNetworkError,
|
|
27
|
-
AuthInvalidCredentialError,
|
|
28
27
|
} from "../../domain/errors/AuthError";
|
|
29
28
|
import type { AuthConfig } from "../../domain/value-objects/AuthConfig";
|
|
30
29
|
import { DEFAULT_AUTH_CONFIG } from "../../domain/value-objects/AuthConfig";
|
|
@@ -42,7 +41,7 @@ function validateEmail(email: string): boolean {
|
|
|
42
41
|
*/
|
|
43
42
|
function validatePassword(
|
|
44
43
|
password: string,
|
|
45
|
-
config: Required<Omit<AuthConfig, "onUserCreated" | "onUserUpdated" | "onSignOut">>
|
|
44
|
+
config: Required<Omit<AuthConfig, "onUserCreated" | "onUserUpdated" | "onSignOut" | "onAccountDeleted">>
|
|
46
45
|
): { valid: boolean; error?: string } {
|
|
47
46
|
if (password.length < config.minPasswordLength) {
|
|
48
47
|
return {
|
|
@@ -84,10 +83,13 @@ function validatePassword(
|
|
|
84
83
|
|
|
85
84
|
/**
|
|
86
85
|
* Map Firebase Auth errors to domain errors
|
|
86
|
+
* Type-safe error mapping
|
|
87
87
|
*/
|
|
88
|
-
function mapFirebaseAuthError(error:
|
|
89
|
-
|
|
90
|
-
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";
|
|
91
93
|
|
|
92
94
|
// Firebase Auth error codes
|
|
93
95
|
if (code === "auth/email-already-in-use") {
|
|
@@ -117,10 +119,6 @@ function mapFirebaseAuthError(error: any): Error {
|
|
|
117
119
|
if (code === "auth/too-many-requests") {
|
|
118
120
|
return new AuthError("Too many requests. Please try again later.", "AUTH_TOO_MANY_REQUESTS");
|
|
119
121
|
}
|
|
120
|
-
// Firebase v9+ uses auth/invalid-credential for both wrong email and wrong password
|
|
121
|
-
if (code === "auth/invalid-credential") {
|
|
122
|
-
return new AuthInvalidCredentialError();
|
|
123
|
-
}
|
|
124
122
|
|
|
125
123
|
return new AuthError(message, code);
|
|
126
124
|
}
|
|
@@ -156,7 +154,7 @@ export class AuthService implements IAuthService {
|
|
|
156
154
|
private getAuth(): Auth | null {
|
|
157
155
|
if (!this.auth) {
|
|
158
156
|
/* eslint-disable-next-line no-console */
|
|
159
|
-
if (__DEV__) {
|
|
157
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
160
158
|
console.warn("Auth service is not initialized. Call initialize() first.");
|
|
161
159
|
}
|
|
162
160
|
return null;
|
|
@@ -179,7 +177,10 @@ export class AuthService implements IAuthService {
|
|
|
179
177
|
}
|
|
180
178
|
|
|
181
179
|
// Validate password
|
|
182
|
-
const passwordValidation = validatePassword(
|
|
180
|
+
const passwordValidation = validatePassword(
|
|
181
|
+
params.password,
|
|
182
|
+
this.config as Required<Omit<AuthConfig, "onUserCreated" | "onUserUpdated" | "onSignOut" | "onAccountDeleted">>
|
|
183
|
+
);
|
|
183
184
|
if (!passwordValidation.valid) {
|
|
184
185
|
throw new AuthWeakPasswordError(passwordValidation.error);
|
|
185
186
|
}
|
|
@@ -207,17 +208,14 @@ export class AuthService implements IAuthService {
|
|
|
207
208
|
// Call user created callback if provided
|
|
208
209
|
if (this.config.onUserCreated) {
|
|
209
210
|
try {
|
|
210
|
-
await this.config.onUserCreated(userCredential.user,
|
|
211
|
-
username: params.username,
|
|
212
|
-
displayName: params.displayName,
|
|
213
|
-
});
|
|
211
|
+
await this.config.onUserCreated(userCredential.user, params.username);
|
|
214
212
|
} catch (callbackError) {
|
|
215
213
|
// Don't fail signup if callback fails
|
|
216
214
|
}
|
|
217
215
|
}
|
|
218
216
|
|
|
219
217
|
return userCredential.user;
|
|
220
|
-
} catch (error:
|
|
218
|
+
} catch (error: unknown) {
|
|
221
219
|
throw mapFirebaseAuthError(error);
|
|
222
220
|
}
|
|
223
221
|
}
|
|
@@ -249,18 +247,8 @@ export class AuthService implements IAuthService {
|
|
|
249
247
|
);
|
|
250
248
|
|
|
251
249
|
this.isGuestMode = false;
|
|
252
|
-
|
|
253
|
-
// Call sign in callback if provided
|
|
254
|
-
if (this.config.onSignIn) {
|
|
255
|
-
try {
|
|
256
|
-
await this.config.onSignIn(userCredential.user);
|
|
257
|
-
} catch (callbackError) {
|
|
258
|
-
// Don't fail signin if callback fails
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
|
|
262
250
|
return userCredential.user;
|
|
263
|
-
} catch (error:
|
|
251
|
+
} catch (error: unknown) {
|
|
264
252
|
throw mapFirebaseAuthError(error);
|
|
265
253
|
}
|
|
266
254
|
}
|
|
@@ -288,7 +276,7 @@ export class AuthService implements IAuthService {
|
|
|
288
276
|
// Don't fail signout if callback fails
|
|
289
277
|
}
|
|
290
278
|
}
|
|
291
|
-
} catch (error:
|
|
279
|
+
} catch (error: unknown) {
|
|
292
280
|
throw mapFirebaseAuthError(error);
|
|
293
281
|
}
|
|
294
282
|
}
|
|
@@ -309,15 +297,6 @@ export class AuthService implements IAuthService {
|
|
|
309
297
|
}
|
|
310
298
|
|
|
311
299
|
this.isGuestMode = true;
|
|
312
|
-
|
|
313
|
-
// Call guest mode enabled callback if provided
|
|
314
|
-
if (this.config.onGuestModeEnabled) {
|
|
315
|
-
try {
|
|
316
|
-
await this.config.onGuestModeEnabled();
|
|
317
|
-
} catch (callbackError) {
|
|
318
|
-
// Don't fail guest mode if callback fails
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
300
|
}
|
|
322
301
|
|
|
323
302
|
/**
|
|
@@ -387,7 +366,7 @@ export function initializeAuthService(
|
|
|
387
366
|
export function getAuthService(): AuthService | null {
|
|
388
367
|
if (!authServiceInstance || !authServiceInstance.isInitialized()) {
|
|
389
368
|
/* eslint-disable-next-line no-console */
|
|
390
|
-
if (__DEV__) {
|
|
369
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
391
370
|
console.warn(
|
|
392
371
|
"Auth service is not initialized. Call initializeAuthService() first."
|
|
393
372
|
);
|