import PocketBase from "pocketbase"; const url = process.env.NEXT_PUBLIC_POCKETBASE_URL || "http://127.0.0.1:8090"; const ADMIN_TOKEN_REFRESH_BUFFER_MS = 60_000; type CachedAdminAuth = { token: string; expiresAt: number; }; let cachedAdminAuth: CachedAdminAuth | null = null; function readJwtExpiryMs(token: string): number { const parts = token.split("."); if (parts.length < 2) return 0; try { const payload = JSON.parse( Buffer.from(parts[1], "base64url").toString("utf8"), ) as { exp?: unknown }; if (typeof payload.exp !== "number" || !Number.isFinite(payload.exp)) { return 0; } return payload.exp * 1000; } catch { return 0; } } function buildPocketBaseWithCachedAuth(): PocketBase | null { if ( !cachedAdminAuth || cachedAdminAuth.expiresAt <= Date.now() + ADMIN_TOKEN_REFRESH_BUFFER_MS ) { return null; } const pb = new PocketBase(url); if (pb.authStore && typeof pb.authStore.save === "function") { pb.authStore.save(cachedAdminAuth.token); return pb; } return null; } export function clearAdminPocketBaseAuthCache(): void { cachedAdminAuth = null; } export async function getAdminPocketBase() { const email = process.env.PB_ADMIN_EMAIL; const password = process.env.PB_ADMIN_PASSWORD; if (!email || !password) { throw new Error("PB_ADMIN_EMAIL and PB_ADMIN_PASSWORD are required for server API operations."); } const cachedPb = buildPocketBaseWithCachedAuth(); if (cachedPb) return cachedPb; const pb = new PocketBase(url); const authData = await pb.collection("_superusers").authWithPassword(email, password); cachedAdminAuth = { token: authData.token, expiresAt: readJwtExpiryMs(authData.token), }; return pb; }