Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

mobile: prevent duplicate subscription

daniel31x13360247988b49
diff --git a/apps/mobile/app/subscribe.tsx b/apps/mobile/app/subscribe.tsx
index 1e218ed0..ce570c70 100644
--- a/apps/mobile/app/subscribe.tsx
+++ b/apps/mobile/app/subscribe.tsx
@@ -15,7 +15,7 @@ import useAuthStore from "@/store/auth";
import { useConfig } from "@linkwarden/router/config";
import { useUser } from "@linkwarden/router/user";
import { Plan } from "@linkwarden/types/global";
-import { router } from "expo-router";
+import { router, useFocusEffect } from "expo-router";
import { useColorScheme } from "nativewind";
import {
Archive,
@@ -35,10 +35,11 @@ import {
useIAP,
type Purchase,
} from "expo-iap";
-import { useEffect, useMemo, useState } from "react";
+import { useCallback, useEffect, useMemo, useState } from "react";
import {
ActivityIndicator,
Alert,
+ AppState,
Image,
Linking,
Platform,
@@ -204,6 +205,24 @@ export default function SubscribeScreen() {
router.replace("/(tabs)/dashboard");
}, [auth.status, isChecking, showSubscribe]);
+ useFocusEffect(
+ useCallback(() => {
+ if (auth.status === "authenticated") refetchUser();
+ }, [auth.status, refetchUser])
+ );
+
+ useEffect(() => {
+ const subscription = AppState.addEventListener("change", (state) => {
+ if (
+ state === "active" &&
+ useAuthStore.getState().auth.status === "authenticated"
+ )
+ refetchUser();
+ });
+
+ return () => subscription.remove();
+ }, [refetchUser]);
+
useEffect(() => {
if (!connected) return;
@@ -384,6 +403,16 @@ export default function SubscribeScreen() {
return;
}
+ const { data: freshUser } = await refetchUser();
+ if (
+ freshUser?.subscription?.active ||
+ freshUser?.parentSubscription?.active
+ ) {
+ router.replace("/(tabs)/dashboard");
+ setPurchaseLoading(false);
+ return;
+ }
+
// The store account may already hold a subscription (reinstall, new device,
// signed-out repurchase attempt) — re-link it instead of buying again.
const existing = await getAvailablePurchases().catch(