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 <noreply@anthropic.com>
This commit is contained in:
yushen 2026-02-13 18:25:37 +08:00
parent 5ee08e9368
commit ec413effb9
2 changed files with 35 additions and 0 deletions

View file

@ -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;
}
}

View file

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