Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,60 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
storage,
);

/**
* Synchronize authentication state across all VS Code windows.
* Fixes Issue #498 by ensuring consistent logout behavior when the session token changes.
*/
async function syncAuth() {
const url = storage.getUrl();
const token = await storage.getSessionToken();

// Update the REST client with current credentials
restClient.setHost(url || "");
restClient.setSessionToken(token || "");

// Determine authentication state
const isAuthenticated = !!(url && token);

// Update VS Code contexts to reflect current auth state
await vscode.commands.executeCommand(
"setContext",
"coder.authenticated",
isAuthenticated,
);

if (!isAuthenticated) {
// Clear owner context since user is not authenticated
await vscode.commands.executeCommand(
"setContext",
"coder.isOwner",
false,
);

// Show consistent logout notification across all windows
vscode.window
.showInformationMessage("You've been logged out of Coder!", "Login")
.then((action) => {
if (action === "Login") {
vscode.commands.executeCommand("coder.login");
}
});

vscode.commands.executeCommand("coder.refreshWorkspaces");
} else {
vscode.commands.executeCommand("coder.refreshWorkspaces");
}
}

// Listen for session token changes to sync auth state across windows
ctx.subscriptions.push(
ctx.secrets.onDidChange((e) => {
if (e.key === "sessionToken") {
syncAuth();
}
}),
);

const myWorkspacesProvider = new WorkspaceProvider(
WorkspaceQuery.Mine,
restClient,
Expand Down