@umituz/react-native-auth 1.9.0 → 1.10.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.9.0",
3
+ "version": "1.10.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 } 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,17 @@ export function useAuthState(): UseAuthStateResult {
33
33
  const [error, setError] = useState<string | null>(null);
34
34
  const [loading, setLoading] = useState(false);
35
35
 
36
+ // Ref to track latest isGuest value for event handlers
37
+ const isGuestRef = useRef(isGuest);
38
+
36
39
  const user = isGuest ? null : mapToAuthUser(firebaseUser);
37
40
  const isAuthenticated = !!user && !isGuest;
38
41
 
42
+ // Keep ref in sync with state
43
+ useEffect(() => {
44
+ isGuestRef.current = isGuest;
45
+ }, [isGuest]);
46
+
39
47
  // Reset guest mode when user signs in
40
48
  useEffect(() => {
41
49
  if (firebaseUser && isGuest) {
@@ -43,38 +51,28 @@ export function useAuthState(): UseAuthStateResult {
43
51
  }
44
52
  }, [firebaseUser, isGuest]);
45
53
 
46
- // Sync isGuest state with service on mount
54
+ // Sync isGuest state with service on mount only
47
55
  useEffect(() => {
48
56
  const service = getAuthService();
49
57
  if (service) {
50
58
  const serviceIsGuest = service.getIsGuestMode();
51
- if (serviceIsGuest !== isGuest) {
59
+ if (serviceIsGuest !== isGuestRef.current) {
52
60
  setIsGuest(serviceIsGuest);
53
61
  }
54
62
  }
55
63
  }, []);
56
64
 
57
- // Listen for guest-mode-enabled event
65
+ // Listen for auth events - subscribe once on mount
58
66
  useEffect(() => {
59
67
  const guestSubscription = DeviceEventEmitter.addListener(
60
68
  "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
- }
69
+ () => setIsGuest(true)
68
70
  );
69
71
 
70
72
  const authSubscription = DeviceEventEmitter.addListener(
71
73
  "user-authenticated",
72
74
  () => {
73
- /* eslint-disable-next-line no-console */
74
- if (__DEV__) {
75
- console.log("[useAuthState] User authenticated event received");
76
- }
77
- if (isGuest) {
75
+ if (isGuestRef.current) {
78
76
  setIsGuest(false);
79
77
  }
80
78
  }
@@ -84,7 +82,7 @@ export function useAuthState(): UseAuthStateResult {
84
82
  guestSubscription.remove();
85
83
  authSubscription.remove();
86
84
  };
87
- }, [isGuest]);
85
+ }, []);
88
86
 
89
87
  return {
90
88
  user,