import type { APIRoute } from "astro"; import { medusaConfig } from "../../../../lib/medusa"; export const prerender = false; /** * Dokument-Download fürs Firmenportal. Ein Link im Browser kann kein * Bearer-Token mitschicken; deshalb holt diese Route die Datei mit dem * Token aus dem Cookie und reicht sie durch. */ export const GET: APIRoute = async ({ params, cookies, redirect }) => { const token = cookies.get("customer_jwt")?.value; if (!token) return redirect(`/konto/anmelden?redirect=/konto/firma`); const { url, key } = medusaConfig(); const res = await fetch(`${url}/store/customers/me/b2b/documents/${encodeURIComponent(params.id ?? "")}`, { headers: { Authorization: `Bearer ${token}`, "x-publishable-api-key": key }, }); if (!res.ok) return new Response("Dokument nicht gefunden.", { status: res.status === 401 ? 401 : 404 }); return new Response(res.body, { headers: { "Content-Type": res.headers.get("Content-Type") ?? "application/octet-stream", "Content-Disposition": res.headers.get("Content-Disposition") ?? "attachment", "Cache-Control": "private, no-store", }, }); };