Skip to content

Commit f512b45

Browse files
committed
refactor: improve auth sync function placement and removed logs
Move syncAuth function and event listener to better location in code Cleaned up logs
1 parent 856fcf5 commit f512b45

File tree

1 file changed

+54
-60
lines changed

1 file changed

+54
-60
lines changed

src/extension.ts

Lines changed: 54 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,60 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
6666
storage,
6767
);
6868

69+
/**
70+
* Synchronize authentication state across all VS Code windows.
71+
* Fixes Issue #498 by ensuring consistent logout behavior when the session token changes.
72+
*/
73+
async function syncAuth() {
74+
const url = storage.getUrl();
75+
const token = await storage.getSessionToken();
76+
77+
// Update the REST client with current credentials
78+
restClient.setHost(url || "");
79+
restClient.setSessionToken(token || "");
80+
81+
// Determine authentication state
82+
const isAuthenticated = !!(url && token);
83+
84+
// Update VS Code contexts to reflect current auth state
85+
await vscode.commands.executeCommand(
86+
"setContext",
87+
"coder.authenticated",
88+
isAuthenticated,
89+
);
90+
91+
if (!isAuthenticated) {
92+
// Clear owner context since user is not authenticated
93+
await vscode.commands.executeCommand(
94+
"setContext",
95+
"coder.isOwner",
96+
false,
97+
);
98+
99+
// Show consistent logout notification across all windows
100+
vscode.window
101+
.showInformationMessage("You've been logged out of Coder!", "Login")
102+
.then((action) => {
103+
if (action === "Login") {
104+
vscode.commands.executeCommand("coder.login");
105+
}
106+
});
107+
108+
vscode.commands.executeCommand("coder.refreshWorkspaces");
109+
} else {
110+
vscode.commands.executeCommand("coder.refreshWorkspaces");
111+
}
112+
}
113+
114+
// Listen for session token changes to sync auth state across windows
115+
ctx.subscriptions.push(
116+
ctx.secrets.onDidChange((e) => {
117+
if (e.key === "sessionToken") {
118+
syncAuth();
119+
}
120+
}),
121+
);
122+
69123
const myWorkspacesProvider = new WorkspaceProvider(
70124
WorkspaceQuery.Mine,
71125
restClient,
@@ -412,64 +466,4 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
412466
}
413467
}
414468
}
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-
storage.output.info("Auth sync called!");
425-
output.info(
426-
`Auth sync triggered: url=${url ? "present" : "none"}, token=${token ? "present" : "none"}`,
427-
);
428-
429-
// Update the REST client with current credentials
430-
restClient.setHost(url || "");
431-
restClient.setSessionToken(token || "");
432-
433-
// Determine authentication state
434-
const isAuthenticated = !!(url && token);
435-
436-
// Update VS Code contexts to reflect current auth state
437-
await vscode.commands.executeCommand(
438-
"setContext",
439-
"coder.authenticated",
440-
isAuthenticated,
441-
);
442-
443-
if (!isAuthenticated) {
444-
// Clear owner context since user is not authenticated
445-
await vscode.commands.executeCommand(
446-
"setContext",
447-
"coder.isOwner",
448-
false,
449-
);
450-
451-
// Show consistent logout notification across all windows
452-
vscode.window
453-
.showInformationMessage("You've been logged out of Coder!", "Login")
454-
.then((action) => {
455-
if (action === "Login") {
456-
vscode.commands.executeCommand("coder.login");
457-
}
458-
});
459-
460-
vscode.commands.executeCommand("coder.refreshWorkspaces");
461-
} else {
462-
vscode.commands.executeCommand("coder.refreshWorkspaces");
463-
}
464-
}
465-
466-
// Listen for session token changes to sync auth state across windows
467-
ctx.subscriptions.push(
468-
ctx.secrets.onDidChange((e) => {
469-
if (e.key === "sessionToken") {
470-
output.info("Session token changed, syncing auth state");
471-
syncAuth();
472-
}
473-
}),
474-
);
475469
}

0 commit comments

Comments
 (0)