Skip to content

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

Tweak native vault sync flow called from React Native (#520)

Leendert de Borst8d3034676bc8
diff --git a/apps/mobile-app/app/initialize.tsx b/apps/mobile-app/app/initialize.tsx
index e9c694312..3a171b682 100644
--- a/apps/mobile-app/app/initialize.tsx
+++ b/apps/mobile-app/app/initialize.tsx
@@ -75,9 +75,7 @@ export default function Initialize() : React.ReactNode {
}
// Vault successfully unlocked - proceed with decryption
- await new Promise(resolve => setTimeout(resolve, 750));
- setStatus(t('app.status.decryptingVault'));
- await new Promise(resolve => setTimeout(resolve, 750));
+ await new Promise(resolve => setTimeout(resolve, 500));
// Migrations pending
if (await dbContext.hasPendingMigrations()) {
@@ -150,9 +148,7 @@ export default function Initialize() : React.ReactNode {
setStatus(t('app.status.unlockingVault'));
const isUnlocked = await dbContext.unlockVault();
if (isUnlocked) {
- await new Promise(resolve => setTimeout(resolve, 750));
- setStatus(t('app.status.decryptingVault'));
- await new Promise(resolve => setTimeout(resolve, 750));
+ await new Promise(resolve => setTimeout(resolve, 500));
// Check if the vault is up to date, if not, redirect to the upgrade page.
if (await dbContext.hasPendingMigrations()) {
diff --git a/apps/mobile-app/app/reinitialize.tsx b/apps/mobile-app/app/reinitialize.tsx
index 8aa3a071d..db265d924 100644
--- a/apps/mobile-app/app/reinitialize.tsx
+++ b/apps/mobile-app/app/reinitialize.tsx
@@ -76,9 +76,7 @@ export default function ReinitializeScreen() : React.ReactNode {
}
// Vault successfully unlocked - proceed with decryption
- await new Promise(resolve => setTimeout(resolve, 1000));
- setStatus(t('app.status.decryptingVault'));
- await new Promise(resolve => setTimeout(resolve, 1000));
+ await new Promise(resolve => setTimeout(resolve, 500));
// Migrations pending
if (await dbContext.hasPendingMigrations()) {
diff --git a/apps/mobile-app/hooks/useVaultSync.ts b/apps/mobile-app/hooks/useVaultSync.ts
index 75ab5b021..6f3eb078d 100644
--- a/apps/mobile-app/hooks/useVaultSync.ts
+++ b/apps/mobile-app/hooks/useVaultSync.ts
@@ -79,22 +79,26 @@ export const useVaultSync = () : {
await new Promise(resolve => setTimeout(resolve, 300));
}
- // Call native syncVault which handles:
- // - Calling Auth/status endpoint
- // - Comparing vault revisions
- // - Downloading vault if needed
- // - Storing encrypted vault
- // - Updating revision number
- // - Setting offline mode
+ // Step 1: Check if a new vault version is available
+ // This calls Auth/status endpoint and compares vault revisions
let hasNewVault = false;
+ let newRevision: number | null = null;
try {
- onStatus?.(t('vault.syncingUpdatedVault'));
- hasNewVault = await NativeVaultManager.syncVault();
-
- // Add artificial delay for initial sync UX
- if (enableDelay && hasNewVault) {
- await new Promise(resolve => setTimeout(resolve, 1000));
+ const versionCheckResult = await NativeVaultManager.isNewVaultVersionAvailable();
+ hasNewVault = versionCheckResult.isNewVersionAvailable;
+ newRevision = versionCheckResult.newRevision;
+
+ // Step 2: If a new version is available, download it
+ if (hasNewVault && newRevision != null) {
+ onStatus?.(t('vault.syncingUpdatedVault'));
+
+ // Run downloadVault with a min delay for UX purposes
+ await withMinimumDelay(
+ () => NativeVaultManager.downloadVault(newRevision!),
+ enableDelay ? 500 : 300,
+ true
+ );
}
} catch (err) {
console.error('VaultSync: syncVault error:', err);
diff --git a/apps/mobile-app/ios/NativeVaultManager/RCTNativeVaultManager.mm b/apps/mobile-app/ios/NativeVaultManager/RCTNativeVaultManager.mm
index a53851547..989d93441 100644
--- a/apps/mobile-app/ios/NativeVaultManager/RCTNativeVaultManager.mm
+++ b/apps/mobile-app/ios/NativeVaultManager/RCTNativeVaultManager.mm
@@ -231,8 +231,12 @@
// MARK: - Vault Sync and Mutate
-- (void)syncVault:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
- [vaultManager syncVault:resolve rejecter:reject];
+- (void)isNewVaultVersionAvailable:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
+ [vaultManager isNewVaultVersionAvailable:resolve rejecter:reject];
+}
+
+- (void)downloadVault:(double)newRevision resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
+ [vaultManager downloadVault:newRevision resolver:resolve rejecter:reject];
}
- (void)mutateVault:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
diff --git a/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift b/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift
index 69a20f419..3051bfd7f 100644
--- a/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift
+++ b/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift
@@ -634,23 +634,62 @@ public class VaultManager: NSObject {
// MARK: - Vault Sync and Mutate
@objc
- func syncVault(_ resolve: @escaping RCTPromiseResolveBlock,
- rejecter reject: @escaping RCTPromiseRejectBlock) {
+ func isNewVaultVersionAvailable(_ resolve: @escaping RCTPromiseResolveBlock,
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
Task {
do {
- let hasNewVault = try await vaultStore.syncVault(using: webApiService)
+ let newRevision = try await vaultStore.isNewVaultVersionAvailable(using: webApiService)
await MainActor.run {
- resolve(hasNewVault)
+ if let revision = newRevision {
+ // Return an object with the new revision number
+ let result: [String: Any] = [
+ "isNewVersionAvailable": true,
+ "newRevision": revision
+ ]
+ resolve(result)
+ } else {
+ // No new version available
+ let result: [String: Any] = [
+ "isNewVersionAvailable": false,
+ "newRevision": NSNull()
+ ]
+ resolve(result)
+ }
+ }
+ } catch {
+ print("VaultManager: Check for new vault version failed: \(error)")
+ await MainActor.run {
+ // Map VaultSyncError to proper error codes for React Native
+ if let syncError = error as? VaultSyncError {
+ reject(syncError.code, syncError.message, error)
+ } else {
+ // Fallback for unknown errors
+ reject("VAULT_CHECK_VERSION_ERROR", "Failed to check vault version: \(error.localizedDescription)", error)
+ }
+ }
+ }
+ }
+ }
+
+ @objc
+ func downloadVault(_ newRevision: Int,
+ resolver resolve: @escaping RCTPromiseResolveBlock,
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
+ Task {
+ do {
+ try await vaultStore.downloadVault(using: webApiService, newRevision: newRevision)
+ await MainActor.run {
+ resolve(true)
}
} catch {
- print("VaultManager: Vault sync failed: \(error)")
+ print("VaultManager: Vault download failed: \(error)")
await MainActor.run {
// Map VaultSyncError to proper error codes for React Native
if let syncError = error as? VaultSyncError {
reject(syncError.code, syncError.message, error)
} else {
// Fallback for unknown errors
- reject("VAULT_SYNC_UNKNOWN_ERROR", "Failed to sync vault: \(error.localizedDescription)", error)
+ reject("VAULT_DOWNLOAD_ERROR", "Failed to download vault: \(error.localizedDescription)", error)
}
}
}
diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Sync.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Sync.swift
index 627e9c84c..31c0b3492 100644
--- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Sync.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Sync.swift
@@ -34,15 +34,37 @@ public struct VaultResponse: Codable {
extension VaultStore {
// MARK: - Vault Sync
- /// Sync the vault with the server
- /// Returns true if a new vault was downloaded, false if vault is already up to date
- public func syncVault(using webApiService: WebApiService) async throws -> Bool {
+ /// Check if a new vault version is available on the server
+ /// Returns the new revision number if available, nil if vault is up to date
+ public func isNewVaultVersionAvailable(using webApiService: WebApiService) async throws -> Int? {
let status = try await fetchAndValidateStatus(using: webApiService)
setOfflineMode(false)
let currentRevision = getCurrentVaultRevisionNumber()
if status.vaultRevision > currentRevision {
- try await downloadAndStoreVault(using: webApiService, newRevision: status.vaultRevision)
+ return status.vaultRevision
+ }
+
+ return nil
+ }
+
+ /// Download and store the vault from the server
+ /// This method assumes a version check has already been performed
+ /// Use this after calling isNewVaultVersionAvailable() to download the vault
+ public func downloadVault(using webApiService: WebApiService, newRevision: Int) async throws {
+ try await downloadAndStoreVault(using: webApiService, newRevision: newRevision)
+ setOfflineMode(false)
+ }
+
+ /// Sync the vault with the server
+ /// Returns true if a new vault was downloaded, false if vault is already up to date
+ /// NOTE: This is a convenience method that combines isNewVaultVersionAvailable and downloadVault
+ /// For better UX control, use isNewVaultVersionAvailable() and downloadVault() separately
+ public func syncVault(using webApiService: WebApiService) async throws -> Bool {
+ // Check if new version is available
+ if let newRevision = try await isNewVaultVersionAvailable(using: webApiService) {
+ // Download the new vault
+ try await downloadVault(using: webApiService, newRevision: newRevision)
return true
}
diff --git a/apps/mobile-app/specs/NativeVaultManager.ts b/apps/mobile-app/specs/NativeVaultManager.ts
index 78bf5a6e4..896b00e1f 100644
--- a/apps/mobile-app/specs/NativeVaultManager.ts
+++ b/apps/mobile-app/specs/NativeVaultManager.ts
@@ -83,7 +83,8 @@ export interface Spec extends TurboModule {
getOfflineMode(): Promise<boolean>;
// Vault sync and mutate
- syncVault(): Promise<boolean>;
+ isNewVaultVersionAvailable(): Promise<{ isNewVersionAvailable: boolean; newRevision: number | null }>;
+ downloadVault(newRevision: number): Promise<boolean>;
mutateVault(): Promise<boolean>;
}