From c2e8b9a1b4601f4c10795e5d51ee27cee5af6a92 Mon Sep 17 00:00:00 2001 From: momen Date: Mon, 18 May 2026 22:55:21 +0800 Subject: [PATCH] fix(account): route forum login through ipc --- electron/main/index.ts | 47 +++++++ src/App.vue | 37 +++++- .../unit/App.account-placeholders.test.ts | 117 ++++++++++++++++++ src/__tests__/unit/flarumAuth.test.ts | 35 ++++++ src/modules/flarumAuth.ts | 8 +- src/vite-env.d.ts | 4 + 6 files changed, 242 insertions(+), 6 deletions(-) create mode 100644 src/__tests__/unit/App.account-placeholders.test.ts create mode 100644 src/__tests__/unit/flarumAuth.test.ts diff --git a/electron/main/index.ts b/electron/main/index.ts index 53cf19d1..0062b016 100644 --- a/electron/main/index.ts +++ b/electron/main/index.ts @@ -55,6 +55,7 @@ import "./backend/install-manager.js"; import "./handle-url-scheme.js"; const logger = pino({ name: "index.ts" }); +const FLARUM_TOKEN_URL = "https://bbs.spark-app.store/api/token"; // The built directory structure // @@ -118,6 +119,52 @@ ipcMain.handle("get-store-filter", (): "spark" | "apm" | "both" => ipcMain.handle("get-app-version", (): string => getAppVersion()); +ipcMain.handle("request-flarum-token", async (_event, payload: unknown) => { + if (!payload || typeof payload !== "object" || Array.isArray(payload)) { + throw new Error("登录信息格式不正确,请重新输入。"); + } + + const credentials = payload as Record; + if ( + typeof credentials.identification !== "string" || + typeof credentials.password !== "string" + ) { + throw new Error("登录信息格式不正确,请重新输入。"); + } + + const response = await fetch(FLARUM_TOKEN_URL, { + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "User-Agent": getUserAgent(), + }, + body: JSON.stringify({ + identification: credentials.identification, + password: credentials.password, + }), + }); + + if (!response.ok) { + throw new Error("论坛登录失败,请检查账号和密码。"); + } + + const data = (await response.json()) as Record; + const userId = data.userId ?? data.user_id; + if ( + typeof data.token !== "string" || + userId === undefined || + userId === null + ) { + throw new Error("论坛登录响应异常,请稍后重试。"); + } + + return { + token: data.token, + userId: String(userId), + }; +}); + const getMainWindowCloseGuardState = (): MainWindowCloseGuardState => ({ installTaskCount: tasks.size, hasRunningUpdateCenterTasks: diff --git a/src/App.vue b/src/App.vue index b47b5898..8f01744f 100644 --- a/src/App.vue +++ b/src/App.vue @@ -62,7 +62,29 @@ @select-category="selectSubCategory" />
-