From 49b94441e697150b3d9849fe89b0d025409f3d5a Mon Sep 17 00:00:00 2001 From: Aaron Lehmann Date: Thu, 12 Dec 2024 14:06:44 -0800 Subject: [PATCH] Suppress login failure dialog on autologin This can be noisy and confusing if login attempts fail for users who don't have Coder accounts. --- src/commands.ts | 23 ++++++++++++++++------- src/extension.ts | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/commands.ts b/src/commands.ts index 6f1c2b58..8ddd6f51 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -143,6 +143,7 @@ export class Commands { const inputUrl = args[0] const inputToken = args[1] const inputLabel = args[2] + const isAutologin = typeof args[3] === "undefined" ? false : Boolean(args[3]) const url = await this.maybeAskUrl(inputUrl) if (!url) { @@ -155,7 +156,7 @@ export class Commands { const label = typeof inputLabel === "undefined" ? toSafeHost(url) : inputLabel // Try to get a token from the user, if we need one, and their user. - const res = await this.maybeAskToken(url, inputToken) + const res = await this.maybeAskToken(url, inputToken, isAutologin) if (!res) { return // The user aborted, or unable to auth. } @@ -202,7 +203,11 @@ export class Commands { * token. Null means the user aborted or we were unable to authenticate with * mTLS (in the latter case, an error notification will have been displayed). */ - private async maybeAskToken(url: string, token: string): Promise<{ user: User; token: string } | null> { + private async maybeAskToken( + url: string, + token: string, + isAutologin: boolean, + ): Promise<{ user: User; token: string } | null> { const restClient = await makeCoderSdk(url, token, this.storage) if (!needToken()) { try { @@ -212,11 +217,15 @@ export class Commands { return { token: "", user } } catch (err) { const message = getErrorMessage(err, "no response from the server") - this.vscodeProposed.window.showErrorMessage("Failed to log in", { - detail: message, - modal: true, - useCustom: true, - }) + if (isAutologin) { + this.storage.writeToCoderOutputChannel(`Failed to log in to Coder server: ${message}`) + } else { + this.vscodeProposed.window.showErrorMessage("Failed to log in to Coder server", { + detail: message, + modal: true, + useCustom: true, + }) + } // Invalid certificate, most likely. return null } diff --git a/src/extension.ts b/src/extension.ts index 92f7296b..565af251 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -221,7 +221,7 @@ export async function activate(ctx: vscode.ExtensionContext): Promise { if (cfg.get("coder.autologin") === true) { const defaultUrl = cfg.get("coder.defaultUrl") || process.env.CODER_URL if (defaultUrl) { - vscode.commands.executeCommand("coder.login", defaultUrl) + vscode.commands.executeCommand("coder.login", defaultUrl, undefined, undefined, "true") } } }