import type { SupabaseClient } from "@supabase/supabase-js"; import { NextResponse } from "next/server"; import type { Database } from "./types"; // Route Handlers under /api/export/* are outside the App Router layout tree, // so app/(app)/layout.tsx's HR gate never runs for them — each one has to // re-establish that the caller is an active HR user itself. RLS is still the // real boundary (an unauthorized session simply reads nothing); this exists // so those routes answer 401/403 instead of handing back an empty workbook. export async function requireHrUser(supabase: SupabaseClient): Promise { const { data: { user }, } = await supabase.auth.getUser(); if (!user) return NextResponse.json({ error: "Nicht angemeldet." }, { status: 401 }); const { data: profile } = await supabase.from("profiles").select("role, is_active").eq("id", user.id).maybeSingle(); if (profile?.role !== "hr" || profile.is_active !== true) { return NextResponse.json({ error: "Nicht berechtigt." }, { status: 403 }); } return null; }