import { NextRequest, NextResponse } from "next/server"; import { createClient } from "@/lib/supabase/server"; /** * GET /api/me * Returns the current user's profile from DB. * Used by client components (analyze page, etc.) to determine plan, role, usage. * Admin gets FULL access to everything — no limits, no restrictions. */ export async function GET(req: NextRequest) { try { const supabase = await createClient(); const { data: { user } } = await supabase.auth.getUser(); if (!user) { return NextResponse.json({ authenticated: false, plan: "free", role: "user", isAdmin: false, analyses_this_month: 0, scan_limit: 10, can_upload: false, can_compare: false, can_export_pdf: false, can_chat: false, can_redline_llm: false, }); } const { data: profile } = await supabase .from("profiles") .select("plan, role, is_banned, analyses_this_month, full_name, email, team_id, created_at") .eq("id", user.id) .single(); const plan = profile?.plan || "free"; const role = profile?.role || "user"; const isAdmin = role === "admin"; // Admin gets EVERYTHING — no limits, no restrictions, period. const hasFullAccess = isAdmin || plan !== "free"; return NextResponse.json({ authenticated: true, id: user.id, email: profile?.email || user.email, full_name: profile?.full_name || "", plan, role, isAdmin, is_banned: profile?.is_banned || false, analyses_this_month: profile?.analyses_this_month || 0, team_id: profile?.team_id || null, created_at: profile?.created_at || null, // Use 999999 instead of Infinity (not valid JSON) scan_limit: isAdmin ? 999999 : plan === "free" ? 10 : 999999, can_upload: hasFullAccess, can_compare: hasFullAccess, can_export_pdf: hasFullAccess, can_chat: hasFullAccess, can_redline_llm: hasFullAccess, can_api_keys: hasFullAccess, can_team: isAdmin || plan === "team", }); } catch (error) { return NextResponse.json({ authenticated: false, plan: "free", role: "user", isAdmin: false, analyses_this_month: 0, scan_limit: 10, can_upload: false, can_compare: false, can_export_pdf: false, can_chat: false, can_redline_llm: false, }); } }