@umituz/react-native-firebase 1.13.10 → 1.13.12

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.10",
3
+ "version": "1.13.12",
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,13 +1,21 @@
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
- import { getFirebaseAuth, isFirebaseAuthInitialized } from "../../infrastructure/config/FirebaseAuthClient";
11
+ import { getFirebaseAuth } from "../../infrastructure/config/FirebaseAuthClient";
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;
11
19
 
12
20
  export interface UseFirebaseAuthResult {
13
21
  /** Current authenticated user from Firebase Auth */
@@ -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
- * No additional state management layer needed.
26
- *
33
+ * Includes retry mechanism to wait for Firebase initialization.
34
+ *
27
35
  * @example
28
36
  * ```typescript
29
37
  * const { user, loading } = useFirebaseAuth();
@@ -34,43 +42,101 @@ export function useFirebaseAuth(): UseFirebaseAuthResult {
34
42
  const [loading, setLoading] = useState(true);
35
43
  const [initialized, setInitialized] = useState(false);
36
44
 
37
- useEffect(() => {
38
- // Check if Firebase Auth is initialized
39
- const isInitialized = isFirebaseAuthInitialized();
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
- setUser(null);
53
- setLoading(false);
54
- return;
54
+ return false;
55
55
  }
56
-
56
+
57
57
  // Subscribe to auth state changes
58
- // onAuthStateChanged fires immediately with current user, then on every change
59
- const unsubscribe = onAuthStateChanged(auth, (currentUser: User | null) => {
58
+ unsubscribeRef.current = onAuthStateChanged(auth, (currentUser: User | null) => {
59
+ if (__DEV__) {
60
+ console.log('[useFirebaseAuth] Auth state changed:', currentUser?.uid || 'null');
61
+ }
60
62
  setUser(currentUser);
61
63
  setLoading(false);
64
+ setInitialized(true);
62
65
  });
63
66
 
67
+ return true;
68
+ } catch {
69
+ return false;
70
+ }
71
+ }, []);
72
+
73
+ useEffect(() => {
74
+ // Try to subscribe immediately - getFirebaseAuth() will auto-initialize
75
+ const auth = getFirebaseAuth();
76
+ if (auth) {
77
+ if (__DEV__) {
78
+ console.log('[useFirebaseAuth] Firebase Auth available, subscribing...');
79
+ }
80
+ setInitialized(true);
81
+ subscribeToAuth();
64
82
  return () => {
65
- unsubscribe();
83
+ if (unsubscribeRef.current) {
84
+ unsubscribeRef.current();
85
+ }
66
86
  };
67
- } catch (error) {
68
- // Firebase Auth not initialized or error
69
- setUser(null);
70
- setLoading(false);
71
- return () => {};
72
87
  }
73
- }, []);
88
+
89
+ // Firebase not ready, start polling with getFirebaseAuth() which auto-initializes
90
+ if (__DEV__) {
91
+ console.log('[useFirebaseAuth] Firebase Auth not available, starting retry...');
92
+ }
93
+
94
+ retryIntervalRef.current = setInterval(() => {
95
+ retryCountRef.current += 1;
96
+
97
+ // getFirebaseAuth() will auto-initialize if Firebase App is available
98
+ const authInstance = getFirebaseAuth();
99
+ if (authInstance) {
100
+ // Firebase Auth is now available, subscribe and stop polling
101
+ if (__DEV__) {
102
+ console.log('[useFirebaseAuth] Firebase Auth available after', retryCountRef.current, 'retries');
103
+ }
104
+
105
+ if (retryIntervalRef.current) {
106
+ clearInterval(retryIntervalRef.current);
107
+ retryIntervalRef.current = null;
108
+ }
109
+
110
+ setInitialized(true);
111
+ subscribeToAuth();
112
+ return;
113
+ }
114
+
115
+ if (retryCountRef.current >= MAX_RETRY_ATTEMPTS) {
116
+ // Max retries reached, stop polling
117
+ if (__DEV__) {
118
+ console.log('[useFirebaseAuth] Max retries reached, Firebase Auth not available');
119
+ }
120
+
121
+ if (retryIntervalRef.current) {
122
+ clearInterval(retryIntervalRef.current);
123
+ retryIntervalRef.current = null;
124
+ }
125
+
126
+ setLoading(false);
127
+ setUser(null);
128
+ }
129
+ }, RETRY_INTERVAL_MS);
130
+
131
+ return () => {
132
+ if (retryIntervalRef.current) {
133
+ clearInterval(retryIntervalRef.current);
134
+ }
135
+ if (unsubscribeRef.current) {
136
+ unsubscribeRef.current();
137
+ }
138
+ };
139
+ }, [subscribeToAuth]);
74
140
 
75
141
  return {
76
142
  user,