Skip to content

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

Tweak mobile app password changed logout and re-login flow (#1473)

Leendert de Borst29e71fff71cf
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt
index 7ec060cba..d9914a18f 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt
@@ -1424,6 +1424,22 @@ class NativeVaultManager(reactContext: ReactApplicationContext) :
}
}
+ /**
+ * Reset sync state to force a fresh download on next sync.
+ * Clears isDirty flag so sync will download instead of trying to merge.
+ * @param promise The promise to resolve when complete.
+ */
+ @ReactMethod
+ override fun resetSyncStateForFreshDownload(promise: Promise) {
+ try {
+ vaultStore.metadata.setIsDirty(false)
+ promise.resolve(null)
+ } catch (e: Exception) {
+ Log.e(TAG, "Error resetting sync state", e)
+ promise.reject("ERR_RESET_SYNC_STATE", "Failed to reset sync state: ${e.message}", e)
+ }
+ }
+
// MARK: - SRP Functions (via Rust Core UniFFI)
/**
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSync.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSync.kt
index cad6e8728..8fd1fbd76 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSync.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSync.kt
@@ -196,6 +196,9 @@ class VaultSync(
)
storeVaultMetadata(vaultMetadata)
+ // Clear dirty flag - we just downloaded fresh from server, no local changes
+ metadata.setIsDirty(false)
+
// Re-unlock if was unlocked
if (database.isVaultUnlocked()) {
// Note: unlock requires auth methods from VaultStore
@@ -619,6 +622,7 @@ class VaultSync(
}
is VaultSyncError.SessionExpired,
is VaultSyncError.AuthenticationFailed,
+ is VaultSyncError.PasswordChanged,
-> {
VaultSyncResult(
success = false,
diff --git a/apps/mobile-app/app/login.tsx b/apps/mobile-app/app/login.tsx
index ac424e841..e1f740f7c 100644
--- a/apps/mobile-app/app/login.tsx
+++ b/apps/mobile-app/app/login.tsx
@@ -52,12 +52,19 @@ export default function LoginScreen() : React.ReactNode {
/**
* Check for saved username (from forced logout) and prefill the username field.
* This enables users to easily re-login after a forced logout.
+ * Only prefill if the username field is empty (user hasn't started typing).
*/
const loadSavedUsername = async () : Promise<void> => {
try {
const savedUsername = await NativeVaultManager.getUsername();
if (savedUsername) {
- setCredentials(prev => ({ ...prev, username: savedUsername }));
+ setCredentials(prev => {
+ // Only prefill if username is empty - don't overwrite user input
+ if (prev.username === '') {
+ return { ...prev, username: savedUsername };
+ }
+ return prev;
+ });
}
} catch {
// Ignore errors - username prefill is optional
@@ -192,22 +199,26 @@ export default function LoginScreen() : React.ReactNode {
/*
* Forced logout recovery check:
* If there's an existing local vault (from forced logout), try to unlock it.
- * If decryption fails (password changed or corrupted), clear the local vault
- * so sync will download fresh from server.
+ * If decryption fails (password changed or corrupted), reset sync state so
+ * sync will do a clean download instead of trying to merge.
*/
const hasExistingVault = await NativeVaultManager.hasEncryptedDatabase();
if (hasExistingVault) {
try {
await NativeVaultManager.unlockVault();
/*
- * Decryption succeeded - local vault is valid, close it for sync to handle.
+ * Decryption succeeded - local vault is valid, sync will handle it.
* The sync will compare revisions and decide whether to keep local or download.
*/
console.info('Existing local vault (after forced logout) decrypted successfully, syncing with server');
} catch {
- // Decryption failed - clear local vault so sync downloads fresh from server
- console.info('Existing vault could not be decrypted (password changed or corrupted), clearing for fresh download');
- await NativeVaultManager.clearVault();
+ /*
+ * Decryption failed (password changed or corrupted).
+ * Reset sync state so sync will do a clean download instead of trying to merge.
+ * This matches the browser extension behavior in persistAndLoadVault().
+ */
+ console.info('Existing vault could not be decrypted (password changed or corrupted), resetting for fresh download');
+ await NativeVaultManager.resetSyncStateForFreshDownload();
}
}
diff --git a/apps/mobile-app/ios/NativeVaultManager/RCTNativeVaultManager.mm b/apps/mobile-app/ios/NativeVaultManager/RCTNativeVaultManager.mm
index 4ccb70bfb..eff9bc5c8 100644
--- a/apps/mobile-app/ios/NativeVaultManager/RCTNativeVaultManager.mm
+++ b/apps/mobile-app/ios/NativeVaultManager/RCTNativeVaultManager.mm
@@ -243,6 +243,10 @@
[vaultManager markVaultClean:(NSInteger)mutationSeqAtStart newServerRevision:(NSInteger)newServerRevision resolver:resolve rejecter:reject];
}
+- (void)resetSyncStateForFreshDownload:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
+ [vaultManager resetSyncStateForFreshDownload:resolve rejecter:reject];
+}
+
// MARK: - PIN Unlock
- (void)isPinEnabled:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
diff --git a/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift b/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift
index f35b24a6a..318783849 100644
--- a/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift
+++ b/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift
@@ -15,7 +15,7 @@ import RustCoreFramework
*/
@objc(VaultManager)
public class VaultManager: NSObject {
- private let vaultStore = VaultStore()
+ private let vaultStore = VaultStore.shared
private let webApiService = WebApiService()
override init() {
@@ -910,6 +910,13 @@ public class VaultManager: NSObject {
resolve(cleared)
}
+ @objc
+ func resetSyncStateForFreshDownload(_ resolve: @escaping RCTPromiseResolveBlock,
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
+ vaultStore.setIsDirty(false)
+ resolve(nil)
+ }
+
// MARK: - SRP (Secure Remote Password) Operations
/// Generate a cryptographic salt for SRP.
diff --git a/apps/mobile-app/ios/VaultStoreKit/Services/WebApiService.swift b/apps/mobile-app/ios/VaultStoreKit/Services/WebApiService.swift
index abb96fe38..033f7f976 100644
--- a/apps/mobile-app/ios/VaultStoreKit/Services/WebApiService.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/Services/WebApiService.swift
@@ -5,7 +5,7 @@ import Foundation
* This service handles authentication, token refresh, and all HTTP operations.
*/
public class WebApiService {
- private let vaultStore = VaultStore()
+ private let vaultStore = VaultStore.shared
// Token storage keys
private let apiUrlKey = "apiUrl"
diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Crypto.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Crypto.swift
index 6eadeca55..f75efccbd 100644
--- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Crypto.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Crypto.swift
@@ -109,11 +109,10 @@ extension VaultStore {
} catch {
print("Decryption failed: \(error)")
- // If the decryption fails, we remove the encryption key from memory
- // so that the next password unlock attempt will require a fresh
- // re-authentication attempt (either via Face ID or passcode) or
- // manual password unlock.
- self.encryptionKey = nil
+ // Note: We intentionally do NOT clear the encryption key here.
+ // The key may be valid for a different vault (e.g., after password change
+ // during login, the new key is stored but the old vault can't be decrypted).
+ // Clearing it would break the subsequent sync that downloads the new vault.
throw NSError(domain: "VaultStore", code: 12, userInfo: [NSLocalizedDescriptionKey: "Decryption failed"])
}
diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Sync.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Sync.swift
index 3eae902ae..5059c9084 100644
--- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Sync.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Sync.swift
@@ -301,10 +301,9 @@ extension VaultStore {
)
try storeVaultMetadata(metadata)
- // Re-unlock if was unlocked
- if isVaultUnlocked {
- try unlockVault()
- }
+ // Clear dirty flag - we just downloaded fresh from server, no local changes
+ setIsDirty(false)
+
setIsSyncing(false)
return VaultSyncResult(
@@ -514,7 +513,7 @@ extension VaultStore {
wasOffline: true,
error: error.message
)
- case .sessionExpired, .authenticationFailed:
+ case .sessionExpired, .authenticationFailed, .passwordChanged:
return VaultSyncResult(
success: false,
action: .error,
diff --git a/apps/mobile-app/specs/NativeVaultManager.ts b/apps/mobile-app/specs/NativeVaultManager.ts
index 36bb719fd..1ddec6047 100644
--- a/apps/mobile-app/specs/NativeVaultManager.ts
+++ b/apps/mobile-app/specs/NativeVaultManager.ts
@@ -29,6 +29,7 @@ export interface Spec extends TurboModule {
// Sync state management (kept for local mutation tracking)
getSyncState(): Promise<{isDirty: boolean; mutationSequence: number; serverRevision: number; isSyncing: boolean}>;
markVaultClean(mutationSeqAtStart: number, newServerRevision: number): Promise<boolean>;
+ resetSyncStateForFreshDownload(): Promise<void>; // Clears isDirty to force clean download on next sync
// Vault SQL operations
executeQuery(query: string, params: (string | number | null)[]): Promise<string[]>;