feat(auth): 添加登录状态检查,

- 未登录时不会自动打开面板命令打开也会显示需要登录
- 登录之后就回自动打开对话面板
This commit is contained in:
Roe-xin
2025-12-29 18:52:56 +08:00
parent 53e91fc5a0
commit f9c9fa1840
3 changed files with 31 additions and 4 deletions

View File

@ -18,8 +18,15 @@ export function activate(context: vscode.ExtensionContext) {
) )
); );
// 自动打开聊天面板 // 检查登录状态,如果已登录则自动打开聊天面板
vscode.authentication.getSession("iccoder", [], { createIfNone: false })
.then((session) => {
if (session) {
vscode.commands.executeCommand("ic-coder.openChat"); vscode.commands.executeCommand("ic-coder.openChat");
}
}, () => {
// 未登录,不做任何操作
});
// 注册命令:打开助手面板 // 注册命令:打开助手面板
const openPanelCommand = vscode.commands.registerCommand( const openPanelCommand = vscode.commands.registerCommand(

View File

@ -21,6 +21,26 @@ export async function showICHelperPanel(
context: vscode.ExtensionContext, context: vscode.ExtensionContext,
viewColumn?: vscode.ViewColumn viewColumn?: vscode.ViewColumn
) { ) {
// 检查用户是否已登录
try {
const session = await vscode.authentication.getSession("iccoder", [], { createIfNone: false });
if (!session) {
vscode.window.showWarningMessage("请先登录后再使用 IC Coder", "立即登录").then((selection) => {
if (selection === "立即登录") {
vscode.commands.executeCommand("ic-coder.login");
}
});
return;
}
} catch (error) {
vscode.window.showWarningMessage("请先登录后再使用 IC Coder", "立即登录").then((selection) => {
if (selection === "立即登录") {
vscode.commands.executeCommand("ic-coder.login");
}
});
return;
}
// 创建WebView面板 // 创建WebView面板
const panel = vscode.window.createWebviewPanel( const panel = vscode.window.createWebviewPanel(
"icCoder", // 面板ID "icCoder", // 面板ID

View File

@ -49,8 +49,8 @@ export class ICCoderAuthenticationProvider
*/ */
async getSessions( async getSessions(
scopes?: readonly string[] scopes?: readonly string[]
): Promise<readonly vscode.AuthenticationSession[]> { ): Promise<vscode.AuthenticationSession[]> {
return this._sessions; return [...this._sessions];
} }
/** /**