updating auth for server component to rig builder

This commit is contained in:
makearmy 2025-09-29 19:00:43 -04:00
parent d0106c0f18
commit e8f62dded4

View file

@ -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 its 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),
}));