import {getServerSession} from "next-auth"; import {authOptions} from "@/lib/api/auth/authOptions"; export async function serverCall( path: string, method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH", body?: unknown ): Promise { const url = `${process.env.INTERNAL_BACKEND_URL ?? "http://localhost:8080"}${path}`; const session = await getServerSession(authOptions); const headers: Record = { "Content-Type": "application/json", }; if (session?.accessToken) { // Use the access token from the session directly // Token refresh is handled by the JWT callback in authOptions.ts headers["Authorization"] = `Bearer ${session.accessToken}`; // console.log("[auth] Using access token from session for API call"); } else { console.warn("[auth] No access token available in session for API call"); } console.log("[api] Calling backend API - [" + method + "]", path, body ?? ""); return fetch(url, { method, headers, body: body ? JSON.stringify(body) : undefined, }); }