@umituz/react-native-subscription 2.13.3 → 2.13.4

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.13.3",
3
+ "version": "2.13.4",
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",
package/src/index.ts CHANGED
@@ -268,6 +268,14 @@ export {
268
268
  type UseCreditCheckerResult,
269
269
  } from "./presentation/hooks/useCreditChecker";
270
270
 
271
+ export {
272
+ useAuthAwarePurchase,
273
+ type UseAuthAwarePurchaseParams,
274
+ type UseAuthAwarePurchaseResult,
275
+ type PurchaseAuthProvider,
276
+ } from "./presentation/hooks/useAuthAwarePurchase";
277
+
278
+
271
279
  export {
272
280
  usePaywallVisibility,
273
281
  type UsePaywallVisibilityResult,
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Auth-Aware Purchase Hook
3
+ * Generic hook that works with any auth provider
4
+ * Ensures user is authenticated before allowing purchases
5
+ */
6
+
7
+ import { useCallback } from "react";
8
+ import type { PurchasesPackage } from "react-native-purchases";
9
+ import { usePremium } from "./usePremium";
10
+
11
+ export interface PurchaseAuthProvider {
12
+ isAuthenticated: () => boolean;
13
+ showAuthModal: () => void;
14
+ }
15
+
16
+ export interface UseAuthAwarePurchaseParams {
17
+ authProvider: PurchaseAuthProvider;
18
+ }
19
+
20
+ export interface UseAuthAwarePurchaseResult {
21
+ handlePurchase: (pkg: PurchasesPackage) => Promise<boolean>;
22
+ handleRestore: () => Promise<boolean>;
23
+ }
24
+
25
+ export const useAuthAwarePurchase = ({
26
+ authProvider,
27
+ }: UseAuthAwarePurchaseParams): UseAuthAwarePurchaseResult => {
28
+ const { purchasePackage, restorePurchase, closePaywall } = usePremium();
29
+
30
+ const handlePurchase = useCallback(
31
+ async (pkg: PurchasesPackage): Promise<boolean> => {
32
+ if (!authProvider.isAuthenticated()) {
33
+ if (__DEV__) {
34
+ console.log(
35
+ "[useAuthAwarePurchase] User not authenticated, opening auth modal",
36
+ );
37
+ }
38
+ closePaywall();
39
+ authProvider.showAuthModal();
40
+ return false;
41
+ }
42
+
43
+ return purchasePackage(pkg);
44
+ },
45
+ [purchasePackage, closePaywall, authProvider],
46
+ );
47
+
48
+ const handleRestore = useCallback(async (): Promise<boolean> => {
49
+ if (!authProvider.isAuthenticated()) {
50
+ if (__DEV__) {
51
+ console.log(
52
+ "[useAuthAwarePurchase] User not authenticated, opening auth modal",
53
+ );
54
+ }
55
+ closePaywall();
56
+ authProvider.showAuthModal();
57
+ return false;
58
+ }
59
+
60
+ return restorePurchase();
61
+ }, [restorePurchase, closePaywall, authProvider]);
62
+
63
+ return {
64
+ handlePurchase,
65
+ handleRestore,
66
+ };
67
+ };