From e41552a2c04bec662f57233cb5aae73871bca833 Mon Sep 17 00:00:00 2001 From: Leendert de Borst Date: Wed, 14 May 2025 11:04:33 +0200 Subject: [PATCH] Fix credential edit password existence check (#840) --- apps/mobile-app/utils/SqliteClient.tsx | 41 ++++++++++++++++++++------ 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/apps/mobile-app/utils/SqliteClient.tsx b/apps/mobile-app/utils/SqliteClient.tsx index 7132a713e..fb46aed21 100644 --- a/apps/mobile-app/utils/SqliteClient.tsx +++ b/apps/mobile-app/utils/SqliteClient.tsx @@ -813,17 +813,40 @@ class SqliteClient { // 4. Update Password if changed if (credential.Password !== existingCredential.Password) { - const passwordQuery = ` - UPDATE Passwords - SET Value = ?, - UpdatedAt = ? + // Check if a password record already exists for this credential, if not, then create one. + const passwordRecordExistsQuery = ` + SELECT Id + FROM Passwords WHERE CredentialId = ?`; + const passwordResults = await this.executeQuery(passwordRecordExistsQuery, [credential.Id]); - await this.executeUpdate(passwordQuery, [ - credential.Password, - currentDateTime, - credential.Id - ]); + if (passwordResults.length === 0) { + // Create a new password record + const passwordQuery = ` + INSERT INTO Passwords (Id, Value, CredentialId, CreatedAt, UpdatedAt, IsDeleted) + VALUES (?, ?, ?, ?, ?, ?)`; + + await this.executeUpdate(passwordQuery, [ + crypto.randomUUID().toUpperCase(), + credential.Password, + credential.Id, + currentDateTime, + currentDateTime, + 0 + ]); + } else { + // Update the existing password record + const passwordQuery = ` + UPDATE Passwords + SET Value = ?, UpdatedAt = ? + WHERE CredentialId = ?`; + + await this.executeUpdate(passwordQuery, [ + credential.Password, + currentDateTime, + credential.Id + ]); + } } await NativeVaultManager.commitTransaction();