⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.
TypeScript bookmarkbookmark-managercollaborationnextjsreact-nativeread-it-laterself-hostedtypescript
feat(mobile): add ensureCloudIsReachable utility and integrate it into authentication flows
daniel31x1391ce8ce3bdb2
diff --git a/apps/mobile/app/index.tsx b/apps/mobile/app/index.tsxindex f92cf545..15544181 100644--- a/apps/mobile/app/index.tsx+++ b/apps/mobile/app/index.tsx@@ -1,5 +1,6 @@import { Button } from "@/components/ui/Button";import { rawTheme, ThemeName } from "@/lib/colors";+import { ensureCloudIsReachable } from "@/lib/ensureCloudIsReachable";import useAuthStore from "@/store/auth";import { isAtLeastInstanceVersion } from "@linkwarden/router/config";import { FontAwesome } from "@expo/vector-icons";@@ -60,11 +61,15 @@ export default function HomeScreen() {const isCheckingOAuth =currentInstanceInfo?.status === "loading" && !buttonAuths;- const openLoginSheet = () => {+ const openLoginSheet = async () => {+ if (!(await ensureCloudIsReachable(instance))) return;+SheetManager.show("login-sheet");};- const openSignUpSheet = () => {+ const openSignUpSheet = async () => {+ if (!(await ensureCloudIsReachable(instance))) return;+SheetManager.show("sign-up-sheet");};@@ -162,8 +167,9 @@ export default function HomeScreen() {accessibilityLabel="Continue with Apple"className="w-full flex-row gap-2 bg-base-100 px-4"disabled={isCheckingOAuth}- onPress={() => {+ onPress={async () => {if (isCheckingOAuth) return;+ if (!(await ensureCloudIsReachable(instance))) return;signInWithApple(instance);}}>@@ -185,8 +191,9 @@ export default function HomeScreen() {accessibilityLabel="Continue with Google"className="w-full flex-row gap-2 bg-base-100 px-4"disabled={isCheckingOAuth}- onPress={() => {+ onPress={async () => {if (isCheckingOAuth) return;+ if (!(await ensureCloudIsReachable(instance))) return;signInWithGoogle(instance);}}>diff --git a/apps/mobile/app/subscribe.tsx b/apps/mobile/app/subscribe.tsxindex 3064868f..475d451a 100644--- a/apps/mobile/app/subscribe.tsx+++ b/apps/mobile/app/subscribe.tsx@@ -4,6 +4,7 @@ import {hasInactiveSubscription,shouldRouteToSubscribe,} from "@/lib/subscription";+import { ensureCloudIsReachable } from "@/lib/ensureCloudIsReachable";import useAuthStore from "@/store/auth";import { useConfig } from "@linkwarden/router/config";import { useUser } from "@linkwarden/router/user";@@ -236,6 +237,9 @@ export default function SubscribeScreen() {setPurchaseLoading(true);try {+ const serverReachable = await ensureCloudIsReachable(auth.instance);+ if (!serverReachable) return;+await Purchases.logIn(user.uuid);if (user.email) await Purchases.setEmail(user.email);await Purchases.invalidateCustomerInfoCache();diff --git a/apps/mobile/lib/ensureCloudIsReachable.ts b/apps/mobile/lib/ensureCloudIsReachable.tsnew file mode 100644index 00000000..8a96e113--- /dev/null+++ b/apps/mobile/lib/ensureCloudIsReachable.ts@@ -0,0 +1,67 @@+import { Alert, Linking } from "react-native";++const cleanInstance = (instance?: string | null) =>+ (instance || "").trim().replace(/\/+$/, "");++const timeout = (ms: number) =>+ new Promise<never>((_, reject) =>+ setTimeout(() => reject(new Error("TIMEOUT")), ms)+ );++export const isServerReachable = async (+ instance?: string | null,+ ms = 30000+) => {+ const nextInstance = cleanInstance(instance);++ if (nextInstance !== "https://cloud.linkwarden.app") return true;++ try {+ const res = await Promise.race([+ fetch(`${nextInstance}/api/v1/config`),+ timeout(ms),+ ]);++ return res.ok;+ } catch {+ return false;+ }+};++export const ensureCloudIsReachable = async (instance?: string | null) => {+ const reachable = await isServerReachable(instance);++ if (!reachable) {+ Alert.alert(+ "Service temporarily unavailable",++ "We can’t reach our servers right now. This may be due to scheduled maintenance or a temporary outage. Please check your connection and try again soon.",++ [+ {+ text: "Contact Support",++ onPress: () => {+ const subject = encodeURIComponent("App connection issue");++ const body = encodeURIComponent(+ "Hi, I'm having trouble connecting to the app. It says the server is unavailable or under maintenance. Can you help?"+ );++ Linking.openURL(+ `mailto:[email protected]?subject=${subject}&body=${body}`+ );+ },+ },++ {+ text: "Close",+ },+ ],++ { cancelable: true }+ );+ }++ return reachable;+};diff --git a/apps/mobile/store/auth.ts b/apps/mobile/store/auth.tsindex 578cb79e..f9dea6eb 100644--- a/apps/mobile/store/auth.ts+++ b/apps/mobile/store/auth.ts@@ -16,6 +16,7 @@ import { clearCache } from "@/lib/cache";import useDataStore from "@/store/data";import { useOfflineSyncStore } from "@/lib/offlineSync";import { hasInactiveSubscription } from "@/lib/subscription";+import { ensureCloudIsReachable } from "@/lib/ensureCloudIsReachable";const cloudInstance = "https://cloud.linkwarden.app";const cloudConfig: Config = {@@ -289,6 +290,8 @@ const useAuthStore = create<AuthStore>((set, get) => ({instance,acceptPromotionalEmails = false,}) => {+ if (!(await ensureCloudIsReachable(instance))) return false;+try {const res = await Promise.race([fetch(`${instance}/api/v1/users`, {@@ -326,6 +329,8 @@ const useAuthStore = create<AuthStore>((set, get) => ({if (process.env.EXPO_PUBLIC_SHOW_LOGS === "true")console.log("Signing into", instance);+ if (!(await ensureCloudIsReachable(instance))) return;+if (token) {try {// make a request to the API to validate the token@@ -411,6 +416,8 @@ const useAuthStore = create<AuthStore>((set, get) => ({}},signInWithApple: async (instance) => {+ if (!(await ensureCloudIsReachable(instance))) return;+let credential: AppleAuthentication.AppleAuthenticationCredential;try {credential = await AppleAuthentication.signInAsync({@@ -473,6 +480,8 @@ const useAuthStore = create<AuthStore>((set, get) => ({}},signInWithGoogle: async (instance) => {+ if (!(await ensureCloudIsReachable(instance))) return;+let idToken: string | null = null;let displayName = "";