@umituz/react-native-firebase 1.13.9 → 1.13.11
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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-firebase",
|
|
3
|
-
"version": "1.13.
|
|
3
|
+
"version": "1.13.11",
|
|
4
4
|
"description": "Unified Firebase package for React Native apps - Auth and Firestore services using Firebase JS SDK (no native modules).",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* useFirebaseAuth Hook
|
|
3
3
|
* React hook for Firebase Auth state management
|
|
4
|
-
*
|
|
4
|
+
*
|
|
5
5
|
* Directly uses Firebase Auth's built-in state management via onAuthStateChanged
|
|
6
|
+
* Includes retry mechanism to wait for Firebase initialization
|
|
6
7
|
*/
|
|
7
8
|
|
|
8
|
-
import { useEffect, useState } from "react";
|
|
9
|
+
import { useEffect, useState, useRef, useCallback } from "react";
|
|
9
10
|
import { onAuthStateChanged, type User } from "firebase/auth";
|
|
10
11
|
import { getFirebaseAuth, isFirebaseAuthInitialized } from "../../infrastructure/config/FirebaseAuthClient";
|
|
11
12
|
|
|
13
|
+
declare const __DEV__: boolean;
|
|
14
|
+
|
|
15
|
+
/** Retry interval in milliseconds */
|
|
16
|
+
const RETRY_INTERVAL_MS = 100;
|
|
17
|
+
/** Maximum retry attempts (100ms * 50 = 5 seconds max wait) */
|
|
18
|
+
const MAX_RETRY_ATTEMPTS = 50;
|
|
19
|
+
|
|
12
20
|
export interface UseFirebaseAuthResult {
|
|
13
21
|
/** Current authenticated user from Firebase Auth */
|
|
14
22
|
user: User | null;
|
|
@@ -20,10 +28,10 @@ export interface UseFirebaseAuthResult {
|
|
|
20
28
|
|
|
21
29
|
/**
|
|
22
30
|
* Hook for Firebase Auth state management
|
|
23
|
-
*
|
|
31
|
+
*
|
|
24
32
|
* Directly uses Firebase Auth's built-in state management.
|
|
25
|
-
*
|
|
26
|
-
*
|
|
33
|
+
* Includes retry mechanism to wait for Firebase initialization.
|
|
34
|
+
*
|
|
27
35
|
* @example
|
|
28
36
|
* ```typescript
|
|
29
37
|
* const { user, loading } = useFirebaseAuth();
|
|
@@ -34,46 +42,98 @@ export function useFirebaseAuth(): UseFirebaseAuthResult {
|
|
|
34
42
|
const [loading, setLoading] = useState(true);
|
|
35
43
|
const [initialized, setInitialized] = useState(false);
|
|
36
44
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
setInitialized(isInitialized);
|
|
41
|
-
|
|
42
|
-
if (!isInitialized) {
|
|
43
|
-
setLoading(false);
|
|
44
|
-
setUser(null);
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
45
|
+
const retryCountRef = useRef(0);
|
|
46
|
+
const unsubscribeRef = useRef<(() => void) | null>(null);
|
|
47
|
+
const retryIntervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
|
47
48
|
|
|
49
|
+
const subscribeToAuth = useCallback(() => {
|
|
48
50
|
try {
|
|
49
51
|
const auth = getFirebaseAuth();
|
|
50
|
-
|
|
52
|
+
|
|
51
53
|
if (!auth) {
|
|
52
|
-
|
|
53
|
-
setLoading(false);
|
|
54
|
-
return;
|
|
54
|
+
return false;
|
|
55
55
|
}
|
|
56
|
-
|
|
56
|
+
|
|
57
57
|
// Subscribe to auth state changes
|
|
58
|
-
|
|
58
|
+
unsubscribeRef.current = onAuthStateChanged(auth, (currentUser: User | null) => {
|
|
59
|
+
if (__DEV__) {
|
|
60
|
+
console.log('[useFirebaseAuth] Auth state changed:', currentUser?.uid || 'null');
|
|
61
|
+
}
|
|
59
62
|
setUser(currentUser);
|
|
60
63
|
setLoading(false);
|
|
64
|
+
setInitialized(true);
|
|
61
65
|
});
|
|
62
66
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
67
|
+
return true;
|
|
68
|
+
} catch {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}, []);
|
|
66
72
|
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
// Try to subscribe immediately if Firebase is ready
|
|
75
|
+
if (isFirebaseAuthInitialized()) {
|
|
76
|
+
if (__DEV__) {
|
|
77
|
+
console.log('[useFirebaseAuth] Firebase already initialized, subscribing...');
|
|
78
|
+
}
|
|
79
|
+
setInitialized(true);
|
|
80
|
+
subscribeToAuth();
|
|
67
81
|
return () => {
|
|
68
|
-
|
|
82
|
+
if (unsubscribeRef.current) {
|
|
83
|
+
unsubscribeRef.current();
|
|
84
|
+
}
|
|
69
85
|
};
|
|
70
|
-
} catch (error) {
|
|
71
|
-
// Firebase Auth not initialized or error
|
|
72
|
-
setUser(null);
|
|
73
|
-
setLoading(false);
|
|
74
|
-
return () => {};
|
|
75
86
|
}
|
|
76
|
-
|
|
87
|
+
|
|
88
|
+
// Firebase not ready, start polling
|
|
89
|
+
if (__DEV__) {
|
|
90
|
+
console.log('[useFirebaseAuth] Firebase not initialized, starting retry...');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
retryIntervalRef.current = setInterval(() => {
|
|
94
|
+
retryCountRef.current += 1;
|
|
95
|
+
|
|
96
|
+
if (isFirebaseAuthInitialized()) {
|
|
97
|
+
// Firebase is now initialized, subscribe and stop polling
|
|
98
|
+
if (__DEV__) {
|
|
99
|
+
console.log('[useFirebaseAuth] Firebase initialized after', retryCountRef.current, 'retries');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (retryIntervalRef.current) {
|
|
103
|
+
clearInterval(retryIntervalRef.current);
|
|
104
|
+
retryIntervalRef.current = null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
setInitialized(true);
|
|
108
|
+
subscribeToAuth();
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (retryCountRef.current >= MAX_RETRY_ATTEMPTS) {
|
|
113
|
+
// Max retries reached, stop polling
|
|
114
|
+
if (__DEV__) {
|
|
115
|
+
console.log('[useFirebaseAuth] Max retries reached, Firebase not initialized');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (retryIntervalRef.current) {
|
|
119
|
+
clearInterval(retryIntervalRef.current);
|
|
120
|
+
retryIntervalRef.current = null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
setLoading(false);
|
|
124
|
+
setUser(null);
|
|
125
|
+
}
|
|
126
|
+
}, RETRY_INTERVAL_MS);
|
|
127
|
+
|
|
128
|
+
return () => {
|
|
129
|
+
if (retryIntervalRef.current) {
|
|
130
|
+
clearInterval(retryIntervalRef.current);
|
|
131
|
+
}
|
|
132
|
+
if (unsubscribeRef.current) {
|
|
133
|
+
unsubscribeRef.current();
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
}, [subscribeToAuth]);
|
|
77
137
|
|
|
78
138
|
return {
|
|
79
139
|
user,
|