@umituz/react-native-subscription 2.37.58 → 2.37.60
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 +1 -1
- package/src/domains/credits/presentation/useCredits.ts +7 -8
- package/src/domains/subscription/application/initializer/ServiceConfigurator.ts +4 -0
- package/src/domains/subscription/infrastructure/hooks/usePurchasePackage.ts +0 -6
- package/src/domains/subscription/infrastructure/utils/authPurchaseState.ts +1 -0
- package/src/domains/subscription/presentation/useAuthAwarePurchase.ts +4 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-subscription",
|
|
3
|
-
"version": "2.37.
|
|
3
|
+
"version": "2.37.60",
|
|
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",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useQuery, useQueryClient } from "@umituz/react-native-design-system";
|
|
2
2
|
import { useCallback, useMemo, useEffect } from "react";
|
|
3
|
-
import { useAuthStore, selectUserId
|
|
3
|
+
import { useAuthStore, selectUserId } from "@umituz/react-native-auth";
|
|
4
4
|
import { subscriptionEventBus, SUBSCRIPTION_EVENTS } from "../../../shared/infrastructure/SubscriptionEventBus";
|
|
5
5
|
import { NO_CACHE_QUERY_CONFIG } from "../../../shared/infrastructure/react-query/queryConfig";
|
|
6
6
|
import { usePreviousUserCleanup } from "../../../shared/infrastructure/react-query/hooks/usePreviousUserCleanup";
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
isCreditsRepositoryConfigured,
|
|
11
11
|
} from "../infrastructure/CreditsRepositoryManager";
|
|
12
12
|
import { calculateSafePercentage, canAffordAmount } from "../utils/creditValidation";
|
|
13
|
-
import {
|
|
13
|
+
import { isAuthenticated } from "../../subscription/utils/authGuards";
|
|
14
14
|
import { creditsQueryKeys } from "./creditsQueryKeys";
|
|
15
15
|
import type { UseCreditsResult, CreditsLoadStatus } from "./useCredits.types";
|
|
16
16
|
|
|
@@ -26,17 +26,16 @@ const deriveLoadStatus = (
|
|
|
26
26
|
|
|
27
27
|
export const useCredits = (): UseCreditsResult => {
|
|
28
28
|
const userId = useAuthStore(selectUserId);
|
|
29
|
-
const isAnonymous = useAuthStore(selectIsAnonymous);
|
|
30
29
|
const isConfigured = isCreditsRepositoryConfigured();
|
|
31
30
|
|
|
32
31
|
const config = isConfigured ? getCreditsConfig() : null;
|
|
33
|
-
const
|
|
34
|
-
const queryEnabled =
|
|
32
|
+
const hasUser = isAuthenticated(userId);
|
|
33
|
+
const queryEnabled = hasUser && isConfigured;
|
|
35
34
|
|
|
36
35
|
const { data, status, error, refetch } = useQuery({
|
|
37
36
|
queryKey: creditsQueryKeys.user(userId),
|
|
38
37
|
queryFn: async () => {
|
|
39
|
-
if (!
|
|
38
|
+
if (!hasUser || !isConfigured) return null;
|
|
40
39
|
|
|
41
40
|
const repository = getCreditsRepository();
|
|
42
41
|
const result = await repository.getCredits(userId);
|
|
@@ -56,7 +55,7 @@ export const useCredits = (): UseCreditsResult => {
|
|
|
56
55
|
usePreviousUserCleanup(userId, queryClient, creditsQueryKeys.user);
|
|
57
56
|
|
|
58
57
|
useEffect(() => {
|
|
59
|
-
if (!
|
|
58
|
+
if (!hasUser) return undefined;
|
|
60
59
|
|
|
61
60
|
const unsubscribe = subscriptionEventBus.on(SUBSCRIPTION_EVENTS.CREDITS_UPDATED, (updatedUserId) => {
|
|
62
61
|
if (updatedUserId === userId) {
|
|
@@ -65,7 +64,7 @@ export const useCredits = (): UseCreditsResult => {
|
|
|
65
64
|
});
|
|
66
65
|
|
|
67
66
|
return unsubscribe;
|
|
68
|
-
}, [userId,
|
|
67
|
+
}, [userId, hasUser, queryClient]);
|
|
69
68
|
|
|
70
69
|
const credits = data ?? null;
|
|
71
70
|
|
|
@@ -68,6 +68,10 @@ export function configureServices(config: SubscriptionInitConfig, apiKey: string
|
|
|
68
68
|
const u = auth.currentUser;
|
|
69
69
|
return !!(u && !u.isAnonymous);
|
|
70
70
|
},
|
|
71
|
+
hasFirebaseUser: () => {
|
|
72
|
+
const auth = getFirebaseAuth();
|
|
73
|
+
return !!(auth?.currentUser);
|
|
74
|
+
},
|
|
71
75
|
showAuthModal,
|
|
72
76
|
});
|
|
73
77
|
|
|
@@ -4,7 +4,6 @@ import { useAlert } from "@umituz/react-native-design-system";
|
|
|
4
4
|
import {
|
|
5
5
|
useAuthStore,
|
|
6
6
|
selectUserId,
|
|
7
|
-
selectIsAnonymous,
|
|
8
7
|
} from "@umituz/react-native-auth";
|
|
9
8
|
import { SubscriptionManager } from "../../infrastructure/managers/SubscriptionManager";
|
|
10
9
|
import { SUBSCRIPTION_QUERY_KEYS } from "./subscriptionQueryKeys";
|
|
@@ -19,7 +18,6 @@ interface PurchaseMutationResult {
|
|
|
19
18
|
|
|
20
19
|
export const usePurchasePackage = () => {
|
|
21
20
|
const userId = useAuthStore(selectUserId);
|
|
22
|
-
const isAnonymous = useAuthStore(selectIsAnonymous);
|
|
23
21
|
const queryClient = useQueryClient();
|
|
24
22
|
const { showSuccess, showError } = useAlert();
|
|
25
23
|
|
|
@@ -29,10 +27,6 @@ export const usePurchasePackage = () => {
|
|
|
29
27
|
throw new Error("User not authenticated");
|
|
30
28
|
}
|
|
31
29
|
|
|
32
|
-
if (isAnonymous) {
|
|
33
|
-
throw new Error("Anonymous users cannot purchase subscriptions");
|
|
34
|
-
}
|
|
35
|
-
|
|
36
30
|
const productId = pkg.product.identifier;
|
|
37
31
|
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
38
32
|
console.log(`[Purchase] Initializing and purchasing. User: ${userId}`);
|
|
@@ -54,10 +54,10 @@ export const useAuthAwarePurchase = (
|
|
|
54
54
|
const authProvider = authPurchaseStateManager.getProvider();
|
|
55
55
|
if (!authProvider) return;
|
|
56
56
|
|
|
57
|
-
const
|
|
57
|
+
const hasUser = authProvider.hasFirebaseUser();
|
|
58
58
|
const hasSavedPurchase = !!authPurchaseStateManager.getSavedPurchase();
|
|
59
59
|
|
|
60
|
-
if (
|
|
60
|
+
if (hasUser && hasSavedPurchase && !isExecutingSavedRef.current) {
|
|
61
61
|
isExecutingSavedRef.current = true;
|
|
62
62
|
executeSavedPurchase().finally(() => {
|
|
63
63
|
isExecutingSavedRef.current = false;
|
|
@@ -73,9 +73,7 @@ export const useAuthAwarePurchase = (
|
|
|
73
73
|
return false;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
if (!isAuth) {
|
|
76
|
+
if (!authProvider.hasFirebaseUser()) {
|
|
79
77
|
authPurchaseStateManager.savePurchase(pkg, source || params?.source || "settings");
|
|
80
78
|
authProvider.showAuthModal();
|
|
81
79
|
return false;
|
|
@@ -95,7 +93,7 @@ export const useAuthAwarePurchase = (
|
|
|
95
93
|
return false;
|
|
96
94
|
}
|
|
97
95
|
|
|
98
|
-
if (!authProvider.
|
|
96
|
+
if (!authProvider.hasFirebaseUser()) {
|
|
99
97
|
authProvider.showAuthModal();
|
|
100
98
|
return false;
|
|
101
99
|
}
|