From af884b4ca90e1d728e17c366af84bc17663a057d Mon Sep 17 00:00:00 2001 From: Joaquin Colacci Date: Tue, 28 Nov 2023 15:49:37 +0100 Subject: [PATCH] context: fix error in login after succesful delete --- src/context/appPassword.ts | 2 +- src/context/asyncContext.ts | 13 ++++++++++--- src/context/config.ts | 19 ++++++++++++++++++- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/context/appPassword.ts b/src/context/appPassword.ts index 1c231ce..0d692d1 100644 --- a/src/context/appPassword.ts +++ b/src/context/appPassword.ts @@ -70,6 +70,6 @@ export default class AppPassword { } } - throw new ExtensionError(Errors.invalidAppPassword, "Invalid length."); + throw new ExtensionError(Errors.invalidAppPassword, "Invalid app-password length."); } } \ No newline at end of file diff --git a/src/context/asyncContext.ts b/src/context/asyncContext.ts index 68ab54c..c3d38e4 100644 --- a/src/context/asyncContext.ts +++ b/src/context/asyncContext.ts @@ -288,9 +288,16 @@ export default class AsyncContext extends Context { */ async removeAndSaveProfile(name: string) { try { - this.config.removeAndSaveProfile(name); - const success = await this.reloadContext(); - return success; + await this.config.removeAndSaveProfile(name); + const leftProfileNames = this.getProfileNames(); + console.log("[AsyncContext]", "Left profile names: ", leftProfileNames); + if (leftProfileNames && leftProfileNames.length > 0) { + const success = await this.reloadContext(); + return success; + } else { + this.isReadyPromise = new Promise((res) => res(true)); + return true; + } } catch (err) { throw this.parseErr(err, "Error reloading context."); } diff --git a/src/context/config.ts b/src/context/config.ts index df74c45..9f8de58 100644 --- a/src/context/config.ts +++ b/src/context/config.ts @@ -207,6 +207,7 @@ export class Config { } this.save(); + await this.removeKeychainPassword(name); if (newProfileName) { this.setProfile(newProfileName); @@ -276,7 +277,6 @@ export class Config { resolve(); } }; - keychain.setPassword({ account, password: appPassword, @@ -286,6 +286,23 @@ export class Config { }); } + /// Removes an app-password from the keychain + async removeKeychainPassword(account: string): Promise { + return new Promise((resolve, reject) => { + const cb = (err: KeychainError): void => { + if (err) { + reject(err); + } else { + resolve(); + } + }; + keychain.deletePassword({ + account, + service: KEYCHAIN_SERVICE, + }, cb); + }); + } + /// Gets a keychain app-password for a profile async getKeychainPassword(account: string): Promise { return new Promise((resolve, reject) => {