@umituz/react-native-subscription 2.37.30 → 2.37.32

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-subscription",
3
- "version": "2.37.30",
3
+ "version": "2.37.32",
4
4
  "description": "Complete subscription management with RevenueCat, paywall UI, and credits system for React Native apps",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -17,9 +17,8 @@ export const getCurrentUserId = (getAuth: () => FirebaseAuthLike | null): string
17
17
 
18
18
  if (user.isAnonymous) {
19
19
  if (typeof __DEV__ !== 'undefined' && __DEV__) {
20
- console.log('[SubscriptionAuthListener] Anonymous user - returning undefined (RevenueCat will use its own ID)');
20
+ console.log(`[SubscriptionAuthListener] Anonymous user - using Firebase anonymous UID: ${user.uid}`);
21
21
  }
22
- return undefined;
23
22
  }
24
23
 
25
24
  return user.uid;
@@ -40,13 +39,13 @@ export const setupAuthStateListener = (
40
39
  }
41
40
 
42
41
  return auth.onAuthStateChanged((user) => {
43
- const userId = (user && !user.isAnonymous) ? user.uid : undefined;
42
+ const userId = user ? user.uid : undefined;
44
43
 
45
44
  if (typeof __DEV__ !== 'undefined' && __DEV__) {
46
45
  console.log('[SubscriptionAuthListener] 🔔 Auth state changed:', {
47
46
  hasUser: !!user,
48
47
  isAnonymous: user?.isAnonymous,
49
- userId: userId || '(undefined - anonymous)',
48
+ userId: userId || '(undefined - no user)',
50
49
  });
51
50
  }
52
51
 
@@ -28,6 +28,7 @@ export interface SubscriptionInitConfig {
28
28
  showAuthModal: () => void;
29
29
  onCreditsUpdated: (userId: string) => void;
30
30
  creditPackages: CreditPackageConfig;
31
+ allowAnonymousPurchases?: boolean;
31
32
  timeoutMs: number;
32
33
  authStateTimeoutMs: number;
33
34
  }
@@ -7,7 +7,7 @@ import type { CustomerInfo } from "react-native-purchases";
7
7
  import type { PackageType } from "../../../revenuecat/core/types/RevenueCatTypes";
8
8
 
9
9
  export function configureServices(config: SubscriptionInitConfig, apiKey: string): SubscriptionSyncService {
10
- const { entitlementId, credits, creditPackages, getFirebaseAuth, showAuthModal, onCreditsUpdated, getAnonymousUserId } = config;
10
+ const { entitlementId, credits, creditPackages, getFirebaseAuth, showAuthModal, onCreditsUpdated, getAnonymousUserId, allowAnonymousPurchases } = config;
11
11
 
12
12
  if (!creditPackages) {
13
13
  throw new Error('[ServiceConfigurator] creditPackages configuration is required');
@@ -59,6 +59,7 @@ export function configureServices(config: SubscriptionInitConfig, apiKey: string
59
59
  }
60
60
 
61
61
  const u = auth.currentUser;
62
+ if (allowAnonymousPurchases && u?.isAnonymous) return true;
62
63
  return !!(u && !u.isAnonymous);
63
64
  },
64
65
  showAuthModal,
@@ -11,7 +11,6 @@ import { useAlert } from "@umituz/react-native-design-system";
11
11
  import {
12
12
  useAuthStore,
13
13
  selectUserId,
14
- selectIsAnonymous,
15
14
  } from "@umituz/react-native-auth";
16
15
  import { SubscriptionManager } from "../../infrastructure/managers/SubscriptionManager";
17
16
  import { SUBSCRIPTION_QUERY_KEYS } from "./subscriptionQueryKeys";
@@ -32,7 +31,6 @@ interface PurchaseMutationResult {
32
31
  */
33
32
  export const usePurchasePackage = () => {
34
33
  const userId = useAuthStore(selectUserId);
35
- const isAnonymous = useAuthStore(selectIsAnonymous);
36
34
  const queryClient = useQueryClient();
37
35
  const { showSuccess, showError } = useAlert();
38
36
 
@@ -42,10 +40,6 @@ export const usePurchasePackage = () => {
42
40
  throw new Error("User not authenticated");
43
41
  }
44
42
 
45
- if (isAnonymous) {
46
- throw new Error("Anonymous users cannot purchase subscriptions");
47
- }
48
-
49
43
  const productId = pkg.product.identifier;
50
44
  if (typeof __DEV__ !== "undefined" && __DEV__) {
51
45
  console.log("[Purchase] Calling SubscriptionManager.purchasePackage()");
@@ -17,6 +17,11 @@ export function useFeatureGate(params: UseFeatureGateParams): UseFeatureGateResu
17
17
 
18
18
  const pendingActionRef = useRef<(() => void | Promise<void>) | null>(null);
19
19
  const prevCreditBalanceRef = useRef(creditBalance);
20
+ // Separate ref to track previous subscription state for canExecutePurchaseAction.
21
+ // NOTE: Must NOT use hasSubscriptionRef from useSyncedRefs here because useSyncedRefs
22
+ // effects run BEFORE this effect (React runs effects in definition order), so
23
+ // hasSubscriptionRef.current would already be the NEW value when we check it.
24
+ const prevHasSubscriptionRef = useRef(hasSubscription);
20
25
  const isWaitingForPurchaseRef = useRef(false);
21
26
  const isWaitingForAuthCreditsRef = useRef(false);
22
27
 
@@ -50,13 +55,14 @@ export function useFeatureGate(params: UseFeatureGateParams): UseFeatureGateResu
50
55
  }, [isCreditsLoaded, creditBalance, hasSubscription, requiredCredits, onShowPaywallRef, requiredCreditsRef]);
51
56
 
52
57
  useEffect(() => {
53
-
58
+ // Use prevHasSubscriptionRef (updated AFTER check) not hasSubscriptionRef from useSyncedRefs
59
+ // (which is already updated to new value before this effect runs - race condition fix)
54
60
  const shouldExecute = canExecutePurchaseAction(
55
61
  isWaitingForPurchaseRef.current,
56
62
  creditBalance,
57
63
  prevCreditBalanceRef.current ?? 0,
58
64
  hasSubscription,
59
- hasSubscriptionRef.current,
65
+ prevHasSubscriptionRef.current,
60
66
  !!pendingActionRef.current
61
67
  );
62
68
 
@@ -67,9 +73,10 @@ export function useFeatureGate(params: UseFeatureGateParams): UseFeatureGateResu
67
73
  action();
68
74
  }
69
75
 
76
+ // Update AFTER check so next render has correct "prev" values
70
77
  prevCreditBalanceRef.current = creditBalance;
71
- // hasSubscriptionRef is already synced by useSyncedRefs, no need to update manually
72
- // eslint-disable-next-line react-hooks/exhaustive-deps
78
+ prevHasSubscriptionRef.current = hasSubscription;
79
+
73
80
  }, [creditBalance, hasSubscription]);
74
81
 
75
82
  const requireFeature = useCallback(