feat: 集成 VSCode Authentication API 实现用户登录

- 新增 Authentication Provider,登录信息显示在左下角
- 支持浏览器登录并自动回调
- 登录/登出后自动刷新窗口
- 侧边栏根据登录状态显示不同按钮
This commit is contained in:
Roe-xin
2025-12-29 18:25:21 +08:00
parent 4288607ee2
commit 53e91fc5a0
5 changed files with 1104 additions and 5 deletions

View File

@ -3,10 +3,21 @@ import { ICViewProvider } from "./views/ICViewProvider";
import { showICHelperPanel } from "./panels/ICHelperPanel";
import { VCDViewerPanel } from "./panels/VCDViewerPanel";
import { ChatHistoryManager } from "./utils/chatHistoryManager";
import { ICCoderAuthenticationProvider } from "./services/icCoderAuthProvider";
export function activate(context: vscode.ExtensionContext) {
console.log("🎉 IC Coder 插件已激活!");
// 注册 Authentication Provider
const authProvider = new ICCoderAuthenticationProvider(context);
context.subscriptions.push(
vscode.authentication.registerAuthenticationProvider(
"iccoder",
"IC Coder",
authProvider
)
);
// 自动打开聊天面板
vscode.commands.executeCommand("ic-coder.openChat");
@ -54,6 +65,40 @@ export function activate(context: vscode.ExtensionContext) {
}
);
// 注册命令:用户登录
const loginCommand = vscode.commands.registerCommand(
"ic-coder.login",
async () => {
try {
await vscode.authentication.getSession("iccoder", [], { createIfNone: true });
} catch (error) {
vscode.window.showErrorMessage(`登录失败: ${error}`);
}
}
);
// 注册命令:用户登出
const logoutCommand = vscode.commands.registerCommand(
"ic-coder.logout",
async () => {
try {
const session = await vscode.authentication.getSession("iccoder", [], { createIfNone: false });
if (session) {
// 通过创建新会话并清除偏好来实现登出
await vscode.authentication.getSession("iccoder", [], {
clearSessionPreference: true,
forceNewSession: true
});
vscode.window.showInformationMessage("已退出登录");
} else {
vscode.window.showInformationMessage("当前未登录");
}
} catch (error) {
vscode.window.showInformationMessage("当前未登录");
}
}
);
// 注册命令:查看会话历史
// TODO: 这些命令需要根据新的任务架构重新实现
// 暂时注释掉,等待重新实现
@ -102,7 +147,7 @@ export function activate(context: vscode.ExtensionContext) {
*/
// 注册侧边栏视图
const viewProvider = new ICViewProvider(context.extensionUri);
const viewProvider = new ICViewProvider(context.extensionUri, context);
const viewRegistration = vscode.window.registerWebviewViewProvider(
"ic-coder.mainView",
viewProvider
@ -113,6 +158,8 @@ export function activate(context: vscode.ExtensionContext) {
openPanelCommand,
openChatCommand,
openVCDViewerCommand,
loginCommand,
logoutCommand,
// TODO: 等待重新实现这些命令
// viewHistoryCommand,
// newSessionCommand,