fix: clear expired auth state before relogin

This commit is contained in:
Roe-xin
2026-03-02 14:51:11 +08:00
parent 5fc0fd2a95
commit f700473967
3 changed files with 44 additions and 11 deletions

View File

@ -161,18 +161,29 @@ export async function activate(context: vscode.ExtensionContext) {
"ic-coder.login", "ic-coder.login",
async () => { async () => {
try { try {
// 先清除 session 偏好,避免 VSCode 弹出"账户不一致"确认框 const session = await vscode.authentication.getSession("iccoder", [], {
try { createIfNone: false,
await vscode.authentication.getSession("iccoder", [], { });
clearSessionPreference: true, const expired = session?.accessToken
createIfNone: false ? isTokenExpired(session.accessToken)
}); : null;
} catch {
// 忽略错误 // 会话仍有效时,直接打开聊天面板
if (session && expired !== true) {
vscode.commands.executeCommand("ic-coder.openChat");
return;
} }
// 创建新 session // 1) 清空当前登录状态信息
await vscode.authentication.getSession("iccoder", [], { createIfNone: true }); await authProvider.clearSessionsForRelogin();
await context.globalState.update("icCoderSessions", []);
await context.globalState.update("icCoderUserInfo", undefined);
// 2) 重新登录(强制新会话)
await vscode.authentication.getSession("iccoder", [], {
clearSessionPreference: true,
forceNewSession: true,
});
} catch (error) { } catch (error) {
vscode.window.showErrorMessage(`登录失败: ${error}`); vscode.window.showErrorMessage(`登录失败: ${error}`);
} }

View File

@ -176,6 +176,28 @@ export class ICCoderAuthenticationProvider
} }
} }
/**
* Clear local authentication state without window reload.
* Used by re-login flow when session is expired.
*/
async clearSessionsForRelogin(): Promise<void> {
if (this._sessions.length === 0) {
await clearUserInfo();
return;
}
const removed = [...this._sessions];
this._sessions = [];
await this.saveSessions();
await clearUserInfo();
this._onDidChangeSessions.fire({
added: [],
removed,
changed: [],
});
}
/** /**
* 生成会话 ID * 生成会话 ID
*/ */

View File

@ -256,7 +256,7 @@ export class ICViewProvider implements vscode.WebviewViewProvider {
vscode.commands.executeCommand("ic-coder.login"); vscode.commands.executeCommand("ic-coder.login");
} else if (message.command === "logout") { } else if (message.command === "logout") {
// 退出登录(前端已有确认对话框) // 退出登录(前端已有确认对话框)
vscode.commands.executeCommand('iccoder.logout'); vscode.commands.executeCommand("ic-coder.logout");
} else if (message.command === "openICCoder") { } else if (message.command === "openICCoder") {
// 打开 IC Coder 官网 // 打开 IC Coder 官网
vscode.env.openExternal(vscode.Uri.parse('https://www.iccoder.com')); vscode.env.openExternal(vscode.Uri.parse('https://www.iccoder.com'));