Skip to content

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

Remember and auto-prompt last unlock method for browser extension (#2096)

Leendert de Borste48b0228c44a
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 ec80ac176..80a0e60b7 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx
@@ -37,12 +37,13 @@ import type { MobileLoginResult } from '@/utils/types/messaging/MobileLoginResul
import { vaultStateEvents } from '@/events/VaultStateEvents';
/**
- * Unlock mode type
+ * Concrete unlock screens the page can render. Mobile-app unlock is shown
+ * as a modal on top of one of these screens, not as its own mode.
*/
type UnlockMode = 'pin' | 'password';
/**
- * Unified unlock page that handles both PIN and password unlock
+ * Unified unlock page that handles PIN, password, and mobile-app unlock.
*/
const Unlock: React.FC = () => {
const { t } = useTranslation();
@@ -78,7 +79,7 @@ const Unlock: React.FC = () => {
const [error, setError] = useState<string | null>(null);
const { showLoading, hideLoading, setIsInitialLoading } = useLoading();
- // Mobile unlock state
+ // Mobile-unlock modal state
const [showMobileUnlockModal, setShowMobileUnlockModal] = useState(false);
// Logout confirmation state
@@ -128,28 +129,37 @@ const Unlock: React.FC = () => {
* Initialize unlock page - check status and PIN availability
*/
const initialize = async (): Promise<void> => {
- // First check PIN availability and set initial mode
- const [pinEnabled, pinLength] = await Promise.all([
+ const [pinEnabled, pinLength, lastUsed, storedAttempts] = await Promise.all([
isPinEnabled(),
getPinLength(),
+ LocalPreferencesService.getLastUsedUnlockMethod(),
+ LocalPreferencesService.getPasswordUnlockFailedAttempts(),
]);
setPinAvailable(pinEnabled);
setPinLength(pinLength || 6);
+ setPasswordFailedAttempts(storedAttempts);
+
+ // Check API status to know if mobile-unlock is reachable (it needs the server).
+ const statusResult = await checkStatus();
+ const mobileAvailable = statusResult.online;
- // Default to PIN mode if available, otherwise password
- if (pinEnabled) {
+ /*
+ * Pick the initial screen from the last-used method, falling back to
+ * PIN-if-enabled, else password. If the user last unlocked with mobile,
+ * auto-open the QR modal over the base screen.
+ */
+ const baseScreen: UnlockMode = pinEnabled ? 'pin' : 'password';
+ if (lastUsed === 'pin' && pinEnabled) {
setUnlockMode('pin');
- } else {
+ } else if (lastUsed === 'password') {
setUnlockMode('password');
+ } else {
+ setUnlockMode(baseScreen);
+ }
+ if (lastUsed === 'mobile' && mobileAvailable) {
+ setShowMobileUnlockModal(true);
}
-
- // Load password failed attempts counter
- const storedAttempts = await LocalPreferencesService.getPasswordUnlockFailedAttempts();
- setPasswordFailedAttempts(storedAttempts);
-
- // Then check API status
- await checkStatus();
};
initialize();
@@ -316,6 +326,7 @@ const Unlock: React.FC = () => {
// Reset PIN and password failed attempts on successful unlock
await resetFailedAttempts();
await LocalPreferencesService.resetPasswordUnlockFailedAttempts();
+ await LocalPreferencesService.setLastUsedUnlockMethod('password');
setPasswordFailedAttempts(0);
/*
@@ -427,6 +438,7 @@ const Unlock: React.FC = () => {
// Clear dismiss until
await LocalPreferencesService.setVaultLockedDismissUntil(0);
+ await LocalPreferencesService.setLastUsedUnlockMethod('pin');
/*
* Navigate to reinitialize which will call syncVault to sync with server.
@@ -575,6 +587,7 @@ const Unlock: React.FC = () => {
// Reset PIN and password failed attempts on successful unlock
await resetFailedAttempts();
await LocalPreferencesService.resetPasswordUnlockFailedAttempts();
+ await LocalPreferencesService.setLastUsedUnlockMethod('mobile');
setPasswordFailedAttempts(0);
/*
@@ -708,12 +721,24 @@ const Unlock: React.FC = () => {
</div>
{/* Use Password Button */}
- <div className="mt-4">
- <div className="text-center text-sm text-gray-500 dark:text-gray-400 mt-6">
- <button type="button" onClick={switchToPassword} className="text-primary-600 hover:text-primary-700 dark:text-primary-500 dark:hover:text-primary-400 hover:underline font-medium">{t('auth.useMasterPassword')}</button>
- </div>
+ <div className="text-center text-sm text-gray-500 dark:text-gray-400 mt-6">
+ <button type="button" onClick={switchToPassword} className="text-primary-600 hover:text-primary-700 dark:text-primary-500 dark:hover:text-primary-400 hover:underline font-medium">{t('auth.useMasterPassword')}</button>
</div>
</div>
+
+ <MobileUnlockModal
+ isOpen={showMobileUnlockModal}
+ onClose={() => setShowMobileUnlockModal(false)}
+ onSuccess={handleMobileUnlockSuccess}
+ webApi={webApi}
+ mode="unlock"
+ />
+
+ <LogoutConfirmModal
+ isOpen={showLogoutConfirm}
+ onClose={() => setShowLogoutConfirm(false)}
+ onConfirm={handleLogout}
+ />
</div>
);
}
@@ -767,7 +792,7 @@ const Unlock: React.FC = () => {
{t('auth.unlockVault')}
</Button>
- {/* Mobile Unlock Button - only show when server is online */}
+ {/* Mobile Unlock Button (only shown when server is online as it requires the server) */}
{!dbContext.isOffline && (
<button
type="button"
@@ -785,15 +810,14 @@ const Unlock: React.FC = () => {
{t('auth.switchAccounts')} <button type="button" onClick={handleLogoutClick} className="text-primary-600 hover:text-primary-700 dark:text-primary-500 dark:hover:text-primary-400 hover:underline font-medium">{t('common.logout')}</button>
</div>
</form>
- </div>
- {pinAvailable && (
- <div className="text-center text-sm text-gray-500 dark:text-gray-400 mt-6">
- <button type="button" onClick={switchToPin} className="text-primary-600 hover:text-primary-700 dark:text-primary-500 dark:hover:text-primary-400 hover:underline font-medium">{t('auth.unlockWithPin')}</button>
- </div>
- )}
+ {pinAvailable && (
+ <div className="text-center text-sm text-gray-500 dark:text-gray-400 mt-6">
+ <button type="button" onClick={switchToPin} className="text-primary-600 hover:text-primary-700 dark:text-primary-500 dark:hover:text-primary-400 hover:underline font-medium">{t('auth.unlockWithPin')}</button>
+ </div>
+ )}
+ </div>
- {/* Mobile Unlock Modal */}
<MobileUnlockModal
isOpen={showMobileUnlockModal}
onClose={() => setShowMobileUnlockModal(false)}
@@ -802,7 +826,6 @@ const Unlock: React.FC = () => {
mode="unlock"
/>
- {/* Logout Confirmation Modal */}
<LogoutConfirmModal
isOpen={showLogoutConfirm}
onClose={() => setShowLogoutConfirm(false)}
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/settings/VaultUnlockSettings.tsx b/apps/browser-extension/src/entrypoints/popup/pages/settings/VaultUnlockSettings.tsx
index 3af928c47..f2ece917a 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/settings/VaultUnlockSettings.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/settings/VaultUnlockSettings.tsx
@@ -110,6 +110,8 @@ const VaultUnlockSettings: React.FC = () => {
return;
}
+ setShowPinSetup(false);
+
try {
showLoading();
@@ -127,7 +129,6 @@ const VaultUnlockSettings: React.FC = () => {
await setupPin(newPin, encryptionKey);
setPinEnabled(true);
- setShowPinSetup(false);
setPinSetupStep(1);
setNewPin('');
setConfirmPin('');
diff --git a/apps/browser-extension/src/utils/LocalPreferencesService.ts b/apps/browser-extension/src/utils/LocalPreferencesService.ts
index 625cfe493..64b5f2268 100644
--- a/apps/browser-extension/src/utils/LocalPreferencesService.ts
+++ b/apps/browser-extension/src/utils/LocalPreferencesService.ts
@@ -2,6 +2,11 @@ import { AutofillMatchingMode } from '@/utils/RustCore';
import { storage } from '#imports';
+/**
+ * Concrete unlock methods that can be used to unlock the vault.
+ */
+export type UnlockMethod = 'password' | 'pin' | 'mobile';
+
/*
* Storage keys for local preferences.
* These are defined inline since they're only used by this service.
@@ -37,6 +42,9 @@ const KEYS = {
SHOW_FOLDERS: 'local:aliasvault_show_folders',
AUTO_CLOSE_UNLOCK_POPUP: 'local:aliasvault_auto_close_unlock_popup',
+ // Unlock screen behavior
+ LAST_USED_UNLOCK_METHOD: 'local:aliasvault_last_used_unlock_method',
+
// Login save feature
LOGIN_SAVE_ENABLED: 'local:loginSaveEnabled',
LOGIN_SAVE_AUTO_DISMISS_SECONDS: 'local:loginSaveAutoDismissSeconds',
@@ -87,6 +95,31 @@ export const LocalPreferencesService = {
await storage.setItem(KEYS.AUTO_CLOSE_UNLOCK_POPUP, enabled);
},
+ /*
+ * ============================================
+ * Unlock screen behavior
+ * ============================================
+ */
+
+ /**
+ * Get the last-used unlock method (recorded after a successful unlock).
+ * @returns The last-used method, or null if none has been recorded yet.
+ */
+ async getLastUsedUnlockMethod(): Promise<UnlockMethod | null> {
+ const value = await storage.getItem(KEYS.LAST_USED_UNLOCK_METHOD) as UnlockMethod | null;
+ if (value === 'password' || value === 'pin' || value === 'mobile') {
+ return value;
+ }
+ return null;
+ },
+
+ /**
+ * Set the last-used unlock method.
+ */
+ async setLastUsedUnlockMethod(method: UnlockMethod): Promise<void> {
+ await storage.setItem(KEYS.LAST_USED_UNLOCK_METHOD, method);
+ },
+
/**
* Get the autofill matching mode.
* @returns The matching mode. Defaults to DEFAULT.