@umituz/react-native-auth 1.0.2 → 1.0.4
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,3 +83,11 @@ 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
|
+
|
package/src/index.ts
CHANGED
|
@@ -14,7 +14,9 @@ import {
|
|
|
14
14
|
} from "firebase/auth";
|
|
15
15
|
import type { IAuthService, SignUpParams, SignInParams } from "../../application/ports/IAuthService";
|
|
16
16
|
import {
|
|
17
|
+
AuthError,
|
|
17
18
|
AuthInitializationError,
|
|
19
|
+
AuthConfigurationError,
|
|
18
20
|
AuthValidationError,
|
|
19
21
|
AuthWeakPasswordError,
|
|
20
22
|
AuthInvalidEmailError,
|
|
@@ -22,6 +24,7 @@ import {
|
|
|
22
24
|
AuthWrongPasswordError,
|
|
23
25
|
AuthUserNotFoundError,
|
|
24
26
|
AuthNetworkError,
|
|
27
|
+
AuthInvalidCredentialError,
|
|
25
28
|
} from "../../domain/errors/AuthError";
|
|
26
29
|
import type { AuthConfig } from "../../domain/value-objects/AuthConfig";
|
|
27
30
|
import { DEFAULT_AUTH_CONFIG } from "../../domain/value-objects/AuthConfig";
|
|
@@ -108,6 +111,9 @@ function mapFirebaseAuthError(error: any): Error {
|
|
|
108
111
|
if (code === "auth/wrong-password") {
|
|
109
112
|
return new AuthWrongPasswordError();
|
|
110
113
|
}
|
|
114
|
+
if (code === "auth/invalid-credential") {
|
|
115
|
+
return new AuthInvalidCredentialError();
|
|
116
|
+
}
|
|
111
117
|
if (code === "auth/network-request-failed") {
|
|
112
118
|
return new AuthNetworkError();
|
|
113
119
|
}
|
|
@@ -146,9 +152,13 @@ export class AuthService implements IAuthService {
|
|
|
146
152
|
return this.auth !== null;
|
|
147
153
|
}
|
|
148
154
|
|
|
149
|
-
private getAuth(): Auth {
|
|
155
|
+
private getAuth(): Auth | null {
|
|
150
156
|
if (!this.auth) {
|
|
151
|
-
|
|
157
|
+
/* eslint-disable-next-line no-console */
|
|
158
|
+
if (__DEV__) {
|
|
159
|
+
console.warn("Auth service is not initialized. Call initialize() first.");
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
152
162
|
}
|
|
153
163
|
return this.auth;
|
|
154
164
|
}
|
|
@@ -158,6 +168,9 @@ export class AuthService implements IAuthService {
|
|
|
158
168
|
*/
|
|
159
169
|
async signUp(params: SignUpParams): Promise<User> {
|
|
160
170
|
const auth = this.getAuth();
|
|
171
|
+
if (!auth) {
|
|
172
|
+
throw new AuthInitializationError("Auth service is not initialized");
|
|
173
|
+
}
|
|
161
174
|
|
|
162
175
|
// Validate email
|
|
163
176
|
if (!params.email || !validateEmail(params.email)) {
|
|
@@ -210,6 +223,9 @@ export class AuthService implements IAuthService {
|
|
|
210
223
|
*/
|
|
211
224
|
async signIn(params: SignInParams): Promise<User> {
|
|
212
225
|
const auth = this.getAuth();
|
|
226
|
+
if (!auth) {
|
|
227
|
+
throw new AuthInitializationError("Auth service is not initialized");
|
|
228
|
+
}
|
|
213
229
|
|
|
214
230
|
// Validate email
|
|
215
231
|
if (!params.email || !validateEmail(params.email)) {
|
|
@@ -240,6 +256,11 @@ export class AuthService implements IAuthService {
|
|
|
240
256
|
*/
|
|
241
257
|
async signOut(): Promise<void> {
|
|
242
258
|
const auth = this.getAuth();
|
|
259
|
+
if (!auth) {
|
|
260
|
+
// If auth is not initialized, just clear guest mode
|
|
261
|
+
this.isGuestMode = false;
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
243
264
|
|
|
244
265
|
try {
|
|
245
266
|
await firebaseSignOut(auth);
|
|
@@ -265,7 +286,7 @@ export class AuthService implements IAuthService {
|
|
|
265
286
|
const auth = this.getAuth();
|
|
266
287
|
|
|
267
288
|
// Sign out from Firebase if logged in
|
|
268
|
-
if (auth.currentUser) {
|
|
289
|
+
if (auth && auth.currentUser) {
|
|
269
290
|
try {
|
|
270
291
|
await firebaseSignOut(auth);
|
|
271
292
|
} catch (error) {
|
|
@@ -298,6 +319,11 @@ export class AuthService implements IAuthService {
|
|
|
298
319
|
*/
|
|
299
320
|
onAuthStateChange(callback: (user: User | null) => void): () => void {
|
|
300
321
|
const auth = this.getAuth();
|
|
322
|
+
if (!auth) {
|
|
323
|
+
// Return no-op unsubscribe if auth is not initialized
|
|
324
|
+
callback(null);
|
|
325
|
+
return () => {};
|
|
326
|
+
}
|
|
301
327
|
|
|
302
328
|
return onAuthStateChanged(auth, (user) => {
|
|
303
329
|
// Don't update if in guest mode
|
|
@@ -333,13 +359,17 @@ export function initializeAuthService(
|
|
|
333
359
|
|
|
334
360
|
/**
|
|
335
361
|
* Get auth service instance
|
|
336
|
-
*
|
|
362
|
+
* Returns null if service is not initialized (graceful degradation)
|
|
337
363
|
*/
|
|
338
|
-
export function getAuthService(): AuthService {
|
|
364
|
+
export function getAuthService(): AuthService | null {
|
|
339
365
|
if (!authServiceInstance || !authServiceInstance.isInitialized()) {
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
366
|
+
/* eslint-disable-next-line no-console */
|
|
367
|
+
if (__DEV__) {
|
|
368
|
+
console.warn(
|
|
369
|
+
"Auth service is not initialized. Call initializeAuthService() first."
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
return null;
|
|
343
373
|
}
|
|
344
374
|
return authServiceInstance;
|
|
345
375
|
}
|
|
@@ -40,8 +40,16 @@ export function useAuth(): UseAuthResult {
|
|
|
40
40
|
const [isGuest, setIsGuest] = useState(false);
|
|
41
41
|
|
|
42
42
|
useEffect(() => {
|
|
43
|
+
const service = getAuthService();
|
|
44
|
+
if (!service) {
|
|
45
|
+
// Auth service not initialized
|
|
46
|
+
setUser(null);
|
|
47
|
+
setIsGuest(false);
|
|
48
|
+
setLoading(false);
|
|
49
|
+
return () => {};
|
|
50
|
+
}
|
|
51
|
+
|
|
43
52
|
try {
|
|
44
|
-
const service = getAuthService();
|
|
45
53
|
const unsubscribe = service.onAuthStateChange((currentUser) => {
|
|
46
54
|
setUser(currentUser);
|
|
47
55
|
setIsGuest(service.getIsGuestMode());
|
|
@@ -58,7 +66,7 @@ export function useAuth(): UseAuthResult {
|
|
|
58
66
|
unsubscribe();
|
|
59
67
|
};
|
|
60
68
|
} catch (error) {
|
|
61
|
-
// Auth service
|
|
69
|
+
// Auth service error
|
|
62
70
|
setUser(null);
|
|
63
71
|
setIsGuest(false);
|
|
64
72
|
setLoading(false);
|
|
@@ -68,18 +76,27 @@ export function useAuth(): UseAuthResult {
|
|
|
68
76
|
|
|
69
77
|
const signUp = useCallback(async (email: string, password: string, displayName?: string) => {
|
|
70
78
|
const service = getAuthService();
|
|
79
|
+
if (!service) {
|
|
80
|
+
throw new Error("Auth service is not initialized");
|
|
81
|
+
}
|
|
71
82
|
await service.signUp({ email, password, displayName });
|
|
72
83
|
// State will be updated via onAuthStateChange
|
|
73
84
|
}, []);
|
|
74
85
|
|
|
75
86
|
const signIn = useCallback(async (email: string, password: string) => {
|
|
76
87
|
const service = getAuthService();
|
|
88
|
+
if (!service) {
|
|
89
|
+
throw new Error("Auth service is not initialized");
|
|
90
|
+
}
|
|
77
91
|
await service.signIn({ email, password });
|
|
78
92
|
// State will be updated via onAuthStateChange
|
|
79
93
|
}, []);
|
|
80
94
|
|
|
81
95
|
const signOut = useCallback(async () => {
|
|
82
96
|
const service = getAuthService();
|
|
97
|
+
if (!service) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
83
100
|
await service.signOut();
|
|
84
101
|
setUser(null);
|
|
85
102
|
setIsGuest(false);
|
|
@@ -87,6 +104,10 @@ export function useAuth(): UseAuthResult {
|
|
|
87
104
|
|
|
88
105
|
const continueAsGuest = useCallback(async () => {
|
|
89
106
|
const service = getAuthService();
|
|
107
|
+
if (!service) {
|
|
108
|
+
setIsGuest(true);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
90
111
|
await service.setGuestMode();
|
|
91
112
|
setUser(null);
|
|
92
113
|
setIsGuest(true);
|