haixunMaster/lib/services/threads-auth-guard.ts

47 lines
1.4 KiB
TypeScript
Raw Normal View History

2026-06-21 12:50:31 +00:00
import { getActiveAccountProfile } from "@/lib/account-context";
import { accountHasThreadsToken } from "@/lib/threads-api";
import { probeSession } from "@/lib/threads-browser";
export async function requireActiveThreadsAuth() {
const account = await getActiveAccountProfile();
if (!account || !accountHasThreadsToken(account)) {
return {
ok: false as const,
error: "目前帳號尚未綁定 Threads 官方 API請先到側欄完成綁定",
};
}
return { ok: true as const, account };
}
/** 發文:官方 API 或有效瀏覽器 session 擇一即可。 */
export async function requirePublishAuth() {
const account = await getActiveAccountProfile();
if (!account) {
return {
ok: false as const,
error: "請先在側欄選擇或建立經營帳號",
};
}
if (accountHasThreadsToken(account)) {
return { ok: true as const, account, via: "api" as const };
}
if (!account.storageState) {
return {
ok: false as const,
error:
"此帳號尚未有 Threads 瀏覽器 session。請到「連線設定」頁用 Chrome 擴充同步。",
};
}
const valid = await probeSession(account.storageState);
if (!valid) {
return {
ok: false as const,
error: "Threads 瀏覽器 session 已失效,請重新匯入或在本機重新登入後傳到 server。",
};
}
return { ok: true as const, account, via: "browser" as const };
}