Skip to content

Commit 2cb3a79

Browse files
committed
fix: sync authentication state across VS Code windows
Fixes #498 by implementing event-driven authentication synchronization. When a user logs out from one VS Code window, all other windows now immediately show a clear 'You've been logged out of Coder!' notification instead of confusing errors like 'Invalid argument uriOrString'. Uses ctx.secrets.onDidChange to detect session token changes and syncAuth() to update all windows consistently. - Add syncAuth() function to handle auth state changes - Listen for sessionToken changes via ctx.secrets.onDidChange - Update REST client, VS Code contexts, and workspace providers - Show consistent logout notifications across windows Tested: A/B validation confirms fix eliminates confusing user experience.
1 parent a75342a commit 2cb3a79

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/extension.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,58 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
412412
}
413413
}
414414
}
415+
416+
/**
417+
* Synchronize authentication state across all VS Code windows.
418+
* Fixes Issue #498 by ensuring consistent logout behavior when the session token changes.
419+
*/
420+
async function syncAuth() {
421+
const url = storage.getUrl();
422+
const token = await storage.getSessionToken();
423+
424+
// Update the REST client with current credentials
425+
restClient.setHost(url || "");
426+
restClient.setSessionToken(token || "");
427+
428+
// Determine authentication state
429+
const isAuthenticated = !!(url && token);
430+
431+
// Update VS Code contexts to reflect current auth state
432+
await vscode.commands.executeCommand(
433+
"setContext",
434+
"coder.authenticated",
435+
isAuthenticated,
436+
);
437+
438+
if (!isAuthenticated) {
439+
// Clear owner context since user is not authenticated
440+
await vscode.commands.executeCommand(
441+
"setContext",
442+
"coder.isOwner",
443+
false,
444+
);
445+
446+
// Show consistent logout notification across all windows
447+
vscode.window
448+
.showInformationMessage("You've been logged out of Coder!", "Login")
449+
.then((action) => {
450+
if (action === "Login") {
451+
vscode.commands.executeCommand("coder.login");
452+
}
453+
});
454+
455+
vscode.commands.executeCommand("coder.refreshWorkspaces");
456+
} else {
457+
vscode.commands.executeCommand("coder.refreshWorkspaces");
458+
}
459+
}
460+
461+
// Listen for session token changes to sync auth state across windows
462+
ctx.subscriptions.push(
463+
ctx.secrets.onDidChange((e) => {
464+
if (e.key === "sessionToken") {
465+
syncAuth();
466+
}
467+
}),
468+
);
415469
}

0 commit comments

Comments
 (0)