ClauseGuard / web /app /api /me /route.ts
gaurv007's picture
v4.1: Fix /api/me β€” Infinity β†’ 999999 in JSON, add all capability flags
b360975 verified
raw
history blame
2.44 kB
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,
});
}
}