import { cache } from "react"; import { createClient } from "@/lib/supabase/server"; import type { Role } from "./roles"; export type Profile = { id: string; name: string; role: Role; firma_id: string | null; }; export type ProfileResult = | { status: "unauthenticated" } | { status: "missing-profile" } | { status: "ok"; profile: Profile }; export const loadProfile = cache(async (): Promise => { const supabase = await createClient(); const { data: { user }, } = await supabase.auth.getUser(); if (!user) { return { status: "unauthenticated" }; } const { data, error } = await supabase .from("profiles") .select("id, name, role, firma_id") .eq("id", user.id) .single(); if (error || !data) { return { status: "missing-profile" }; } return { status: "ok", profile: data as Profile }; });