Skip to content

Commit b9da49b

Browse files
committed
Fix logout to clear credentials and add switch deployment command (#760)
- Logout now clears stored credentials via secretsManager.clearAllAuthData() - Add "Switch Deployment" command to change deployments without clearing credentials
1 parent b90a1e5 commit b9da49b

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@
217217
"category": "Coder",
218218
"icon": "$(sign-out)"
219219
},
220+
{
221+
"command": "coder.switchDeployment",
222+
"title": "Switch Deployment",
223+
"category": "Coder",
224+
"icon": "$(arrow-swap)"
225+
},
220226
{
221227
"command": "coder.open",
222228
"title": "Open Workspace",
@@ -297,6 +303,10 @@
297303
"command": "coder.logout",
298304
"when": "coder.authenticated"
299305
},
306+
{
307+
"command": "coder.switchDeployment",
308+
"when": "coder.authenticated"
309+
},
300310
{
301311
"command": "coder.createWorkspace",
302312
"when": "coder.authenticated"
@@ -351,6 +361,10 @@
351361
"command": "coder.logout",
352362
"when": "coder.authenticated && view == myWorkspaces"
353363
},
364+
{
365+
"command": "coder.switchDeployment",
366+
"when": "coder.authenticated && view == myWorkspaces"
367+
},
354368
{
355369
"command": "coder.login",
356370
"when": "!coder.authenticated && view == myWorkspaces"

src/commands.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,8 @@ export class Commands {
7474
}
7575

7676
/**
77-
* Log into the provided deployment. If the deployment URL is not specified,
78-
* ask for it first with a menu showing recent URLs along with the default URL
79-
* and CODER_URL, if those are set.
77+
* Log into a deployment. If already authenticated, this is a no-op.
78+
* If no URL is provided, shows a menu of recent URLs plus defaults.
8079
*/
8180
public async login(args?: {
8281
url?: string;
@@ -85,6 +84,13 @@ export class Commands {
8584
if (this.deploymentManager.isAuthenticated()) {
8685
return;
8786
}
87+
await this.performLogin(args);
88+
}
89+
90+
private async performLogin(args?: {
91+
url?: string;
92+
autoLogin?: boolean;
93+
}): Promise<void> {
8894
this.logger.debug("Logging in");
8995

9096
const currentDeployment = await this.secretsManager.getCurrentDeployment();
@@ -197,7 +203,7 @@ export class Commands {
197203
}
198204

199205
/**
200-
* Log out from the currently logged-in deployment.
206+
* Log out and clear stored credentials, requiring re-authentication on next login.
201207
*/
202208
public async logout(): Promise<void> {
203209
if (!this.deploymentManager.isAuthenticated()) {
@@ -206,8 +212,15 @@ export class Commands {
206212

207213
this.logger.debug("Logging out");
208214

215+
const safeHostname =
216+
this.deploymentManager.getCurrentDeployment()?.safeHostname;
217+
209218
await this.deploymentManager.clearDeployment();
210219

220+
if (safeHostname) {
221+
await this.secretsManager.clearAllAuthData(safeHostname);
222+
}
223+
211224
vscode.window
212225
.showInformationMessage("You've been logged out of Coder!", "Login")
213226
.then((action) => {
@@ -221,6 +234,15 @@ export class Commands {
221234
this.logger.debug("Logout complete");
222235
}
223236

237+
/**
238+
* Switch to a different deployment without clearing credentials.
239+
* If login fails or user cancels, stays on current deployment.
240+
*/
241+
public async switchDeployment(): Promise<void> {
242+
this.logger.debug("Switching deployment");
243+
await this.performLogin();
244+
}
245+
224246
/**
225247
* Create a new workspace for the currently logged-in deployment.
226248
*

src/extension.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
197197
"coder.logout",
198198
commands.logout.bind(commands),
199199
),
200+
vscode.commands.registerCommand(
201+
"coder.switchDeployment",
202+
commands.switchDeployment.bind(commands),
203+
),
200204
vscode.commands.registerCommand("coder.open", commands.open.bind(commands)),
201205
vscode.commands.registerCommand(
202206
"coder.openDevContainer",

0 commit comments

Comments
 (0)