From ec413effb92f1a8a844302ae452b90d367844279 Mon Sep 17 00:00:00 2001 From: yushen Date: Fri, 13 Feb 2026 18:25:37 +0800 Subject: [PATCH] feat(core): add auth-store module to read local auth data Reads sid and deviceId from ~/.super-multica/auth.json for use by tools that need authenticated API access. Co-Authored-By: Claude Opus 4.6 --- packages/core/src/hub/auth-store.ts | 34 +++++++++++++++++++++++++++++ packages/core/src/hub/index.ts | 1 + 2 files changed, 35 insertions(+) create mode 100644 packages/core/src/hub/auth-store.ts diff --git a/packages/core/src/hub/auth-store.ts b/packages/core/src/hub/auth-store.ts new file mode 100644 index 00000000..ff2342ba --- /dev/null +++ b/packages/core/src/hub/auth-store.ts @@ -0,0 +1,34 @@ +import { readFileSync } from "node:fs"; +import { join } from "node:path"; +import { DATA_DIR } from "@multica/utils"; + +const AUTH_FILE_PATH = join(DATA_DIR, "auth.json"); + +export type LocalAuthData = { sid: string; deviceId: string }; + +/** + * Read sid and deviceId from ~/.super-multica/auth.json. + * Returns null if the file is missing, unreadable, or incomplete. + */ +export function getLocalAuth(): LocalAuthData | null { + try { + const raw = readFileSync(AUTH_FILE_PATH, "utf8").trim(); + if (!raw) return null; + + const data = JSON.parse(raw); + if ( + typeof data !== "object" || + data === null || + typeof data.sid !== "string" || + !data.sid || + typeof data.deviceId !== "string" || + !data.deviceId + ) { + return null; + } + + return { sid: data.sid, deviceId: data.deviceId }; + } catch { + return null; + } +} diff --git a/packages/core/src/hub/index.ts b/packages/core/src/hub/index.ts index 0b47e705..671f72bd 100644 --- a/packages/core/src/hub/index.ts +++ b/packages/core/src/hub/index.ts @@ -1,4 +1,5 @@ export { Hub } from "./hub.js"; export type { MessageSource, InboundMessageEvent } from "./hub.js"; export { getHubId } from "./hub-identity.js"; +export { getLocalAuth, type LocalAuthData } from "./auth-store.js"; export type { HubOptions } from "./types.js";