import { cookies } from "next/headers"; /** The signed-in admin (from the backend's /api/auth/me), for the admin's server-rendered pages. */ export type Me = { user: { id: string; name: string; email: string; role: "owner" | "admin" | "editor" | "ministry_leader" | "viewer"; ministryId: string | null }; mfa: boolean; }; const api = (process.env.WEBSITE_API_URL || "http://127.0.0.1:5300").replace(/\/+$/, ""); export const ROLE_NAMES: Record = { owner: "Owner", admin: "Admin", editor: "Editor", ministry_leader: "Ministry leader", viewer: "Viewer", }; /** Null when signed out (or the backend can't say); the page then sends people to sign in. */ export async function loadMe(): Promise { const session = (await cookies()).get("ccw_session")?.value; if (!session) return null; try { const res = await fetch(`${api}/api/auth/me`, { headers: { Cookie: `ccw_session=${encodeURIComponent(session)}` }, cache: "no-store" }); if (res.ok) return (await res.json()) as Me; } catch { // The backend is starting or down: treated as signed out. } return null; } /** Where "Sign in" goes for a page of the admin: Accounts, and back to that page. */ export const signInHref = (returnTo: string) => `/api/auth/signin?returnTo=${encodeURIComponent(returnTo)}`;