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";