Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Refactor mobile app deep linking to support both cold and warm app opens (#1347)

Leendert de Borst267f2d3d17f2
diff --git a/apps/mobile-app/app/(tabs)/settings/index.tsx b/apps/mobile-app/app/(tabs)/settings/index.tsx
index e9ba94ce0..662bf4bc9 100644
--- a/apps/mobile-app/app/(tabs)/settings/index.tsx
+++ b/apps/mobile-app/app/(tabs)/settings/index.tsx
@@ -2,6 +2,7 @@ import { Ionicons } from '@expo/vector-icons';
import { router, useFocusEffect } from 'expo-router';
import { useRef, useState, useCallback } from 'react';
import { StyleSheet, View, ScrollView, TouchableOpacity, Animated, Platform, Alert, Linking } from 'react-native';
+import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { useApiUrl } from '@/utils/ApiUrlUtility';
import { AppInfo } from '@/utils/AppInfo';
@@ -17,7 +18,6 @@ import { InlineSkeletonLoader } from '@/components/ui/InlineSkeletonLoader';
import { TitleContainer } from '@/components/ui/TitleContainer';
import { UsernameDisplay } from '@/components/ui/UsernameDisplay';
import { useApp } from '@/context/AppContext';
-import { useSafeAreaInsets } from 'react-native-safe-area-context';
/**
* Settings screen.
diff --git a/apps/mobile-app/app/(tabs)/settings/qr-confirm.tsx b/apps/mobile-app/app/(tabs)/settings/qr-confirm.tsx
index 05dfb5a57..476c10155 100644
--- a/apps/mobile-app/app/(tabs)/settings/qr-confirm.tsx
+++ b/apps/mobile-app/app/(tabs)/settings/qr-confirm.tsx
@@ -127,6 +127,9 @@ export default function QRConfirmScreen() : React.ReactNode {
[
{
text: t('common.ok'),
+ /**
+ * Go back to the previous screen.
+ */
onPress: (): void => {
router.back();
},
diff --git a/apps/mobile-app/app/(tabs)/settings/qr-scanner.tsx b/apps/mobile-app/app/(tabs)/settings/qr-scanner.tsx
index 953e88533..d3ca871bc 100644
--- a/apps/mobile-app/app/(tabs)/settings/qr-scanner.tsx
+++ b/apps/mobile-app/app/(tabs)/settings/qr-scanner.tsx
@@ -1,7 +1,7 @@
import { Ionicons } from '@expo/vector-icons';
import { CameraView, useCameraPermissions } from 'expo-camera';
import { router, useLocalSearchParams } from 'expo-router';
-import { useEffect, useCallback } from 'react';
+import { useEffect, useCallback, useRef } from 'react';
import { View, Alert, StyleSheet } from 'react-native';
import { useColors } from '@/hooks/useColorScheme';
@@ -59,6 +59,8 @@ export default function QRScannerScreen() : React.ReactNode {
const [permission, requestPermission] = useCameraPermissions();
const [isLoadingAfterScan, setIsLoadingAfterScan] = useMinDurationLoading(false, 500);
const { url } = useLocalSearchParams<{ url?: string }>();
+ const hasProcessedUrl = useRef(false);
+ const processedUrls = useRef(new Set<string>());
// Request camera permission on mount
useEffect(() => {
@@ -146,6 +148,12 @@ export default function QRScannerScreen() : React.ReactNode {
return;
}
+ // Prevent processing the same URL multiple times
+ if (processedUrls.current.has(data)) {
+ console.debug('QR code already processed, ignoring duplicate:', data);
+ return;
+ }
+
// Parse the QR code to determine its type
const parsedData = parseQRCode(data);
@@ -154,13 +162,26 @@ export default function QRScannerScreen() : React.ReactNode {
return;
}
+ // Mark this URL as processed
+ processedUrls.current.add(data);
+
// Validate the request and navigate (with min 500ms loading)
validateAndNavigate(parsedData);
}, [isLoadingAfterScan, validateAndNavigate]);
- // Handle QR code URL passed from deep link (e.g., from native camera)
+ /**
+ * Reset hasProcessedUrl when URL changes to allow processing new URLs.
+ */
+ useEffect(() => {
+ hasProcessedUrl.current = false;
+ }, [url]);
+
+ /**
+ * Handle QR code URL passed from deep link (e.g., from native camera).
+ */
useEffect(() => {
- if (url && typeof url === 'string') {
+ if (url && typeof url === 'string' && !hasProcessedUrl.current) {
+ hasProcessedUrl.current = true;
handleBarcodeScanned({ data: url });
}
}, [url, handleBarcodeScanned]);
diff --git a/apps/mobile-app/app/_layout.tsx b/apps/mobile-app/app/_layout.tsx
index e90b4d5e3..25f3b541b 100644
--- a/apps/mobile-app/app/_layout.tsx
+++ b/apps/mobile-app/app/_layout.tsx
@@ -34,6 +34,7 @@ function RootLayoutNav() : React.ReactNode {
const [bootComplete, setBootComplete] = useState(false);
const [redirectTarget, setRedirectTarget] = useState<string | null>(null);
const hasBooted = useRef(false);
+ const processedDeepLinks = useRef(new Set<string>());
useEffect(() => {
/**
@@ -51,7 +52,22 @@ function RootLayoutNav() : React.ReactNode {
await initI18n();
hasBooted.current = true;
- setRedirectTarget('/initialize');
+ // Check if we have a pending deep link and pass it to initialize
+ const initialUrl = await Linking.getInitialURL();
+ if (initialUrl) {
+ const path = initialUrl
+ .replace('net.aliasvault.app://', '')
+ .replace('aliasvault://', '')
+ .replace('exp+aliasvault://', '');
+
+ if (path.startsWith('mobile-login/') || path.includes('credentials/')) {
+ setRedirectTarget(`/initialize?pendingDeepLink=${encodeURIComponent(initialUrl)}`);
+ } else {
+ setRedirectTarget('/initialize');
+ }
+ } else {
+ setRedirectTarget('/initialize');
+ }
setBootComplete(true);
};
@@ -63,6 +79,15 @@ function RootLayoutNav() : React.ReactNode {
* Handle deep link URL by navigating to the appropriate route.
*/
const handleDeepLink = (url: string) : void => {
+ // Prevent processing the same deep link multiple times
+ if (processedDeepLinks.current.has(url)) {
+ console.debug('Deep link already processed, ignoring duplicate:', url);
+ return;
+ }
+
+ // Mark this URL as processed
+ processedDeepLinks.current.add(url);
+
// Remove all supported URL schemes to get the path
let path = url
.replace('net.aliasvault.app://', '')
@@ -71,7 +96,7 @@ function RootLayoutNav() : React.ReactNode {
// Handle mobile login QR code scans from native camera
if (path.startsWith('mobile-login/')) {
- // Process the QR code
+ // Process the QR code (app already unlocked when listener fires)
router.push(`/(tabs)/settings/qr-scanner?url=${encodeURIComponent(`aliasvault://${path}`)}` as Href);
return;
}
@@ -91,8 +116,6 @@ function RootLayoutNav() : React.ReactNode {
/**
* Redirect to a explicit target page if we have one (in case of non-happy path).
- * Otherwise check for a deep link and simulate stack navigation.
- * If neither is present, we let the router redirect us to the default route.
*/
const redirect = async () : Promise<void> => {
if (!bootComplete) {
@@ -100,20 +123,14 @@ function RootLayoutNav() : React.ReactNode {
}
if (redirectTarget) {
- // If we have an explicit redirect target, we navigate to it. This overrides potential deep link handling.
+ // Navigate to the redirect target (may include pendingDeepLink param)
router.replace(redirectTarget as Href);
- } else {
- // Check if we have an initial URL to handle (deep link from camera, autofill, etc.).
- const initialUrl = await Linking.getInitialURL();
- if (initialUrl) {
- handleDeepLink(initialUrl);
- }
}
};
redirect();
- // Listen for deep links when app is already running
+ // Listen for deep links when app is already running and unlocked
const subscription = Linking.addEventListener('url', ({ url }) => {
handleDeepLink(url);
});
diff --git a/apps/mobile-app/app/initialize.tsx b/apps/mobile-app/app/initialize.tsx
index ebd98b734..3b12633a7 100644
--- a/apps/mobile-app/app/initialize.tsx
+++ b/apps/mobile-app/app/initialize.tsx
@@ -1,5 +1,5 @@
import { Ionicons } from '@expo/vector-icons';
-import { useRouter } from 'expo-router';
+import { Href, useRouter, useLocalSearchParams } from 'expo-router';
import { useEffect, useRef, useState, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { Alert, StyleSheet, TouchableOpacity, View } from 'react-native';
@@ -18,6 +18,7 @@ import NativeVaultManager from '@/specs/NativeVaultManager';
*/
export default function Initialize() : React.ReactNode {
const router = useRouter();
+ const { pendingDeepLink } = useLocalSearchParams<{ pendingDeepLink?: string }>();
const [status, setStatus] = useState('');
const [showSkipButton, setShowSkipButton] = useState(false);
const hasInitialized = useRef(false);
@@ -65,6 +66,35 @@ export default function Initialize() : React.ReactNode {
}
}, []);
+ /**
+ * Handle pending deep link after successful unlock.
+ */
+ const handlePendingDeepLink = useCallback((deepLink: string): void => {
+ // Remove all supported URL schemes to get the path
+ let path = deepLink
+ .replace('net.aliasvault.app://', '')
+ .replace('aliasvault://', '')
+ .replace('exp+aliasvault://', '');
+
+ // Handle mobile login QR code scans from native camera
+ if (path.startsWith('mobile-login/')) {
+ router.replace(`/(tabs)/settings/qr-scanner?url=${encodeURIComponent(`aliasvault://${path}`)}` as Href);
+ return;
+ }
+
+ // Handle credential detail routes
+ const isDetailRoute = path.includes('credentials/');
+ if (isDetailRoute) {
+ // First go to the credentials tab.
+ router.replace('/(tabs)/credentials');
+
+ // Then push the target route inside the credentials tab.
+ setTimeout(() => {
+ router.push(path as Href);
+ }, 0);
+ }
+ }, [router]);
+
/**
* Handle offline scenario - show alert with options to open local vault or retry sync.
*/
@@ -72,7 +102,11 @@ export default function Initialize() : React.ReactNode {
// Don't show the alert if we're already in offline mode
if (app.isOffline) {
console.debug('Already in offline mode, skipping offline flow alert');
- router.replace('/(tabs)/credentials');
+ if (pendingDeepLink) {
+ handlePendingDeepLink(pendingDeepLink);
+ } else {
+ router.replace('/(tabs)/credentials');
+ }
return;
}
@@ -127,8 +161,12 @@ export default function Initialize() : React.ReactNode {
return;
}
- // Success - navigate to credentials
- router.replace('/(tabs)/credentials');
+ // Success - check for pending deep link or navigate to credentials
+ if (pendingDeepLink) {
+ handlePendingDeepLink(pendingDeepLink);
+ } else {
+ router.replace('/(tabs)/credentials');
+ }
} catch (err) {
console.error('Error during offline vault unlock:', err);
router.replace('/unlock');
@@ -169,7 +207,7 @@ export default function Initialize() : React.ReactNode {
}
]
);
- }, [dbContext, router, app, t, updateStatus]);
+ }, [dbContext, router, app, t, updateStatus, pendingDeepLink, handlePendingDeepLink]);
useEffect(() => {
// Ensure this only runs once.
@@ -272,8 +310,13 @@ export default function Initialize() : React.ReactNode {
* Handle successful vault sync.
*/
onSuccess: async () => {
- // Vault already unlocked, just navigate to credentials
- router.replace('/(tabs)/credentials');
+ // Check if we have a pending deep link to process
+ if (pendingDeepLink) {
+ handlePendingDeepLink(pendingDeepLink);
+ } else {
+ // Vault already unlocked, just navigate to credentials
+ router.replace('/(tabs)/credentials');
+ }
},
/**
* Handle offline state and prompt user for action.
@@ -317,7 +360,7 @@ export default function Initialize() : React.ReactNode {
clearTimeout(skipButtonTimeoutRef.current);
}
};
- }, [dbContext, syncVault, app, router, t, handleOfflineFlow, updateStatus]);
+ }, [dbContext, syncVault, app, router, t, handleOfflineFlow, updateStatus, pendingDeepLink, handlePendingDeepLink]);
/**
* Handle skip button press by calling the offline handler.