@umituz/react-native-auth 1.9.0 → 1.11.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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-auth",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"description": "Authentication service for React Native apps - Secure, type-safe, and production-ready. Provider-agnostic design supports Firebase Auth and can be adapted for Supabase or other providers.",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Single Responsibility: Manage authentication state
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import { useState, useEffect } from "react";
|
|
6
|
+
import { useState, useEffect, useRef, useMemo } from "react";
|
|
7
7
|
import { DeviceEventEmitter } from "react-native";
|
|
8
8
|
import { getAuthService } from "../../infrastructure/services/AuthService";
|
|
9
9
|
import { useFirebaseAuth } from "@umituz/react-native-firebase-auth";
|
|
@@ -33,9 +33,22 @@ export function useAuthState(): UseAuthStateResult {
|
|
|
33
33
|
const [error, setError] = useState<string | null>(null);
|
|
34
34
|
const [loading, setLoading] = useState(false);
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
// Ref to track latest isGuest value for event handlers
|
|
37
|
+
const isGuestRef = useRef(isGuest);
|
|
38
|
+
|
|
39
|
+
// Memoize user to prevent new object reference on every render
|
|
40
|
+
const user = useMemo(() => {
|
|
41
|
+
if (isGuest) return null;
|
|
42
|
+
return mapToAuthUser(firebaseUser);
|
|
43
|
+
}, [isGuest, firebaseUser?.uid]);
|
|
44
|
+
|
|
37
45
|
const isAuthenticated = !!user && !isGuest;
|
|
38
46
|
|
|
47
|
+
// Keep ref in sync with state
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
isGuestRef.current = isGuest;
|
|
50
|
+
}, [isGuest]);
|
|
51
|
+
|
|
39
52
|
// Reset guest mode when user signs in
|
|
40
53
|
useEffect(() => {
|
|
41
54
|
if (firebaseUser && isGuest) {
|
|
@@ -43,38 +56,28 @@ export function useAuthState(): UseAuthStateResult {
|
|
|
43
56
|
}
|
|
44
57
|
}, [firebaseUser, isGuest]);
|
|
45
58
|
|
|
46
|
-
// Sync isGuest state with service on mount
|
|
59
|
+
// Sync isGuest state with service on mount only
|
|
47
60
|
useEffect(() => {
|
|
48
61
|
const service = getAuthService();
|
|
49
62
|
if (service) {
|
|
50
63
|
const serviceIsGuest = service.getIsGuestMode();
|
|
51
|
-
if (serviceIsGuest !==
|
|
64
|
+
if (serviceIsGuest !== isGuestRef.current) {
|
|
52
65
|
setIsGuest(serviceIsGuest);
|
|
53
66
|
}
|
|
54
67
|
}
|
|
55
68
|
}, []);
|
|
56
69
|
|
|
57
|
-
// Listen for
|
|
70
|
+
// Listen for auth events - subscribe once on mount
|
|
58
71
|
useEffect(() => {
|
|
59
72
|
const guestSubscription = DeviceEventEmitter.addListener(
|
|
60
73
|
"guest-mode-enabled",
|
|
61
|
-
() =>
|
|
62
|
-
/* eslint-disable-next-line no-console */
|
|
63
|
-
if (__DEV__) {
|
|
64
|
-
console.log("[useAuthState] Guest mode enabled event received");
|
|
65
|
-
}
|
|
66
|
-
setIsGuest(true);
|
|
67
|
-
}
|
|
74
|
+
() => setIsGuest(true)
|
|
68
75
|
);
|
|
69
76
|
|
|
70
77
|
const authSubscription = DeviceEventEmitter.addListener(
|
|
71
78
|
"user-authenticated",
|
|
72
79
|
() => {
|
|
73
|
-
|
|
74
|
-
if (__DEV__) {
|
|
75
|
-
console.log("[useAuthState] User authenticated event received");
|
|
76
|
-
}
|
|
77
|
-
if (isGuest) {
|
|
80
|
+
if (isGuestRef.current) {
|
|
78
81
|
setIsGuest(false);
|
|
79
82
|
}
|
|
80
83
|
}
|
|
@@ -84,7 +87,7 @@ export function useAuthState(): UseAuthStateResult {
|
|
|
84
87
|
guestSubscription.remove();
|
|
85
88
|
authSubscription.remove();
|
|
86
89
|
};
|
|
87
|
-
}, [
|
|
90
|
+
}, []);
|
|
88
91
|
|
|
89
92
|
return {
|
|
90
93
|
user,
|