Skip to content

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

Add unlock redirect hook with path restore (#520)

Leendert de Borst7a65678ba22d
diff --git a/apps/browser-extension/src/entrypoints/popup/hooks/useVaultLockRedirect.ts b/apps/browser-extension/src/entrypoints/popup/hooks/useVaultLockRedirect.ts
new file mode 100644
index 000000000..4c5cb8175
--- /dev/null
+++ b/apps/browser-extension/src/entrypoints/popup/hooks/useVaultLockRedirect.ts
@@ -0,0 +1,54 @@
+import { useEffect } from 'react';
+import { useLocation, useNavigate } from 'react-router-dom';
+
+import { useDb } from '@/entrypoints/popup/context/DbContext';
+
+import { storage } from '#imports';
+
+const PENDING_REDIRECT_URL_KEY = 'session:pendingRedirectUrl';
+
+/**
+ * Hook to handle vault lock redirects.
+ * Automatically redirects to unlock page if vault is locked,
+ * preserving the current URL for restoration after unlock.
+ */
+export function useVaultLockRedirect(options: { enabled?: boolean } = {}): { isLocked: boolean } {
+ const { enabled = true } = options;
+ const location = useLocation();
+ const navigate = useNavigate();
+ const { dbInitialized, dbAvailable } = useDb();
+
+ useEffect(() => {
+ if (!enabled || !dbInitialized) {
+ return;
+ }
+
+ // Check if vault is locked
+ if (!dbAvailable) {
+ // Store the full current URL (pathname + search) for restoration after unlock
+ const currentUrl = `${location.pathname}${location.search}`;
+ storage.setItem(PENDING_REDIRECT_URL_KEY, currentUrl);
+
+ // Navigate to unlock without redirect in URL - we use storage instead
+ navigate('/unlock');
+ }
+ }, [enabled, dbInitialized, dbAvailable, location, navigate]);
+
+ return {
+ isLocked: dbInitialized && !dbAvailable
+ };
+}
+
+/**
+ * Get and clear the pending redirect URL from storage.
+ * Used by Reinitialize page to restore user's intended destination after unlock.
+ *
+ * @returns The pending redirect URL, or null if none exists
+ */
+export async function consumePendingRedirectUrl(): Promise<string | null> {
+ const url = await storage.getItem<string>(PENDING_REDIRECT_URL_KEY);
+ if (url) {
+ await storage.removeItem(PENDING_REDIRECT_URL_KEY);
+ }
+ return url;
+}
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx b/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx
index 184f7fd0c..90f3b5923 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx
@@ -5,6 +5,7 @@ import { sendMessage } from 'webext-bridge/popup';
import { useApp } from '@/entrypoints/popup/context/AppContext';
import { useDb } from '@/entrypoints/popup/context/DbContext';
import { useLoading } from '@/entrypoints/popup/context/LoadingContext';
+import { consumePendingRedirectUrl } from '@/entrypoints/popup/hooks/useVaultLockRedirect';
import { useVaultSync } from '@/entrypoints/popup/hooks/useVaultSync';
import { storage } from '#imports';
@@ -78,11 +79,20 @@ const Reinitialize: React.FC = () => {
}, [navigate]);
useEffect(() => {
- // Check for inline unlock mode
- const urlParams = new URLSearchParams(window.location.search);
- const inlineUnlock = urlParams.get('mode') === 'inline_unlock';
-
- if (isFullyInitialized) {
+ /**
+ * Handle initialization and redirect logic
+ */
+ const handleInitialization = async (): Promise<void> => {
+ // Check for inline unlock mode
+ const urlParams = new URLSearchParams(window.location.search);
+ const inlineUnlock = urlParams.get('mode') === 'inline_unlock';
+
+ // Check for pending redirect URL in storage (set by useVaultLockRedirect hook)
+ const pendingRedirectUrl = await consumePendingRedirectUrl();
+
+ if (!isFullyInitialized) {
+ return;
+ }
// Prevent multiple vault syncs (only run sync once)
const shouldRunSync = !hasInitialized.current;
@@ -110,6 +120,10 @@ const Reinitialize: React.FC = () => {
if (inlineUnlock) {
setIsInitialLoading(false);
navigate('/unlock-success', { replace: true });
+ } else if (pendingRedirectUrl) {
+ // If there's a pending redirect URL in storage, use it (most reliable)
+ setIsInitialLoading(false);
+ navigate(pendingRedirectUrl, { replace: true });
} else {
await restoreLastPage();
}
@@ -138,7 +152,9 @@ const Reinitialize: React.FC = () => {
setIsInitialLoading(false);
restoreLastPage();
}
- }
+ };
+
+ handleInitialization();
}, [isFullyInitialized, requiresAuth, isLoggedIn, dbAvailable, navigate, setIsInitialLoading, syncVault, restoreLastPage]);
// This component doesn't render anything visible - it just handles initialization
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx b/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx
index 204e7d3ad..65c132f84 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx
@@ -134,7 +134,6 @@ const Unlock: React.FC = () => {
// Clear dismiss until (which can be enabled after user has dimissed vault is locked popup) to ensure popup is shown.
await storage.setItem(VAULT_LOCKED_DISMISS_UNTIL_KEY, 0);
- // Redirect to reinitialize page
navigate('/reinitialize', { replace: true });
} catch (err) {
// Check if it's a version incompatibility error
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyAuthenticate.tsx b/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyAuthenticate.tsx
index e19139197..73da0d8c9 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyAuthenticate.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyAuthenticate.tsx
@@ -1,11 +1,12 @@
import React, { useEffect, useState } from 'react';
-import { useLocation, useNavigate } from 'react-router-dom';
+import { useLocation } from 'react-router-dom';
import { sendMessage } from 'webext-bridge/popup';
import Button from '@/entrypoints/popup/components/Button';
import LoadingSpinner from '@/entrypoints/popup/components/LoadingSpinner';
import { useDb } from '@/entrypoints/popup/context/DbContext';
import { useLoading } from '@/entrypoints/popup/context/LoadingContext';
+import { useVaultLockRedirect } from '@/entrypoints/popup/hooks/useVaultLockRedirect';
import { useVaultMutate } from '@/entrypoints/popup/hooks/useVaultMutate';
import { PasskeyAuthenticator } from '@/utils/passkey/PasskeyAuthenticator';
@@ -17,7 +18,6 @@ import type { GetRequest, PasskeyGetCredentialResponse, PendingPasskeyGetRequest
*/
const PasskeyAuthenticate: React.FC = () => {
const location = useLocation();
- const navigate = useNavigate();
const { setIsInitialLoading } = useLoading();
const dbContext = useDb();
const { executeVaultMutation } = useVaultMutate();
@@ -26,6 +26,7 @@ const PasskeyAuthenticate: React.FC = () => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [availablePasskeys, setAvailablePasskeys] = useState<Array<{ id: string; displayName: string; username?: string | null }>>([]);
+ const { isLocked } = useVaultLockRedirect();
useEffect(() => {
/**
@@ -37,12 +38,8 @@ const PasskeyAuthenticate: React.FC = () => {
return;
}
- // Check if vault is unlocked
- if (!dbContext.dbAvailable) {
- // Vault is locked, redirect to unlock
- const params = new URLSearchParams(location.search);
- const requestId = params.get('requestId');
- navigate(`/unlock?redirect=/credentials/passkeys/authenticate&requestId=${requestId}`);
+ // If vault is locked, the hook will handle redirect, we just return
+ if (isLocked) {
return;
}
@@ -97,7 +94,7 @@ const PasskeyAuthenticate: React.FC = () => {
};
fetchRequestData();
- }, [location, setIsInitialLoading, dbContext.dbInitialized, dbContext.dbAvailable, dbContext.sqliteClient, navigate]);
+ }, [location, setIsInitialLoading, dbContext.dbInitialized, isLocked, dbContext.sqliteClient]);
/**
* Handle passkey authentication
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyCreate.tsx b/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyCreate.tsx
index e92d98ed5..f30f972b1 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyCreate.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/passkeys/PasskeyCreate.tsx
@@ -1,5 +1,5 @@
import React, { useEffect, useState } from 'react';
-import { useLocation, useNavigate } from 'react-router-dom';
+import { useLocation } from 'react-router-dom';
import { sendMessage } from 'webext-bridge/popup';
import Button from '@/entrypoints/popup/components/Button';
@@ -7,6 +7,7 @@ import { FormInput } from '@/entrypoints/popup/components/FormInput';
import LoadingSpinner from '@/entrypoints/popup/components/LoadingSpinner';
import { useDb } from '@/entrypoints/popup/context/DbContext';
import { useLoading } from '@/entrypoints/popup/context/LoadingContext';
+import { useVaultLockRedirect } from '@/entrypoints/popup/hooks/useVaultLockRedirect';
import { useVaultMutate } from '@/entrypoints/popup/hooks/useVaultMutate';
import { PasskeyAuthenticator } from '@/utils/passkey/PasskeyAuthenticator';
@@ -18,13 +19,13 @@ import type { CreateRequest, PasskeyCreateCredentialResponse, PendingPasskeyCrea
*/
const PasskeyCreate: React.FC = () => {
const location = useLocation();
- const navigate = useNavigate();
const { setIsInitialLoading } = useLoading();
const dbContext = useDb();
const { executeVaultMutation, isLoading: isMutating, syncStatus } = useVaultMutate();
const [request, setRequest] = useState<PendingPasskeyCreateRequest | null>(null);
const [displayName, setDisplayName] = useState('My Passkey');
const [error, setError] = useState<string | null>(null);
+ const { isLocked } = useVaultLockRedirect();
useEffect(() => {
/**
@@ -36,12 +37,8 @@ const PasskeyCreate: React.FC = () => {
return;
}
- // Check if vault is unlocked
- if (!dbContext.dbAvailable) {
- // Vault is locked, redirect to unlock
- const params = new URLSearchParams(location.search);
- const requestId = params.get('requestId');
- navigate(`/unlock?redirect=/credentials/passkeys/create&requestId=${requestId}`);
+ // If vault is locked, the hook will handle redirect, we just return
+ if (isLocked) {
return;
}
@@ -70,7 +67,7 @@ const PasskeyCreate: React.FC = () => {
};
fetchRequestData();
- }, [location, setIsInitialLoading, dbContext.dbInitialized, dbContext.dbAvailable, navigate]);
+ }, [location, setIsInitialLoading, dbContext.dbInitialized, isLocked]);
/**
* Handle passkey creation