From e8f62dded4e7d6e481342e738d730e234684f97a Mon Sep 17 00:00:00 2001 From: makearmy Date: Mon, 29 Sep 2025 19:00:43 -0400 Subject: [PATCH] updating auth for server component to rig builder --- app/rigs/RigBuilderServer.tsx | 41 ++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/app/rigs/RigBuilderServer.tsx b/app/rigs/RigBuilderServer.tsx index ea8ac882..acece3a6 100644 --- a/app/rigs/RigBuilderServer.tsx +++ b/app/rigs/RigBuilderServer.tsx @@ -2,28 +2,43 @@ import { cookies } from "next/headers"; import { redirect } from "next/navigation"; import RigBuilderClient from "./RigBuilderClient"; -import { dxGET } from "@/lib/directus"; type Opt = { id: string | number; label: string }; -function getBearerFromCookies(): string | null { - const jar = cookies(); - const token = jar.get("ma_at")?.value; - return token ? `Bearer ${token}` : null; -} - export default async function RigBuilderServer() { - const bearer = getBearerFromCookies(); - if (!bearer) { + const jar = cookies(); + const ma_at = jar.get("ma_at")?.value; + if (!ma_at) { const next = encodeURIComponent("/rigs"); redirect(`/auth/sign-in?next=${next}`); } - const rows = await dxGET<{ - data: Array<{ id: string | number; name?: string }>; - }>("/items/user_rig_type?fields=id,name&sort=sort", bearer); + const DIRECTUS = (process.env.DIRECTUS_URL || "").replace(/\/$/, ""); + // Fetch rig types on the SERVER with the user bearer + const res = await fetch( + `${DIRECTUS}/items/user_rig_type?fields=id,name&sort=sort`, + { + headers: { + // Build the bearer header explicitly from the raw cookie token + Authorization: `Bearer ${ma_at}`, + Accept: "application/json", + "Cache-Control": "no-store", + }, + cache: "no-store", + } + ); - const rigTypes: Opt[] = (rows?.data ?? []).map((r) => ({ + if (!res.ok) { + // If perms or token are wrong, fail loud so it’s obvious in dev + // (You can swap this for a quieter empty list if you prefer) + const text = await res.text().catch(() => ""); + throw new Error( + `Failed to load user_rig_type (${res.status}): ${text || res.statusText}` + ); + } + + const json = await res.json().catch(() => ({ data: [] as any[] })); + const rigTypes: Opt[] = (json?.data ?? []).map((r: any) => ({ id: r.id, label: r.name ?? String(r.id), }));