gaurv007 commited on
Commit
f16b685
·
verified ·
1 Parent(s): efd3fc1

v3.0: Add /api/me route — returns user profile (plan, role, usage) from DB for client components

Browse files
Files changed (1) hide show
  1. web/app/api/me/route.ts +59 -0
web/app/api/me/route.ts ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { NextRequest, NextResponse } from "next/server";
2
+ import { createClient } from "@/lib/supabase/server";
3
+
4
+ /**
5
+ * GET /api/me
6
+ * Returns the current user's profile from DB.
7
+ * Used by client components (analyze page, etc.) to determine plan, role, usage.
8
+ * No hardcoded emails — everything comes from the database.
9
+ */
10
+ export async function GET(req: NextRequest) {
11
+ try {
12
+ const supabase = await createClient();
13
+ const { data: { user } } = await supabase.auth.getUser();
14
+
15
+ if (!user) {
16
+ return NextResponse.json({
17
+ authenticated: false,
18
+ plan: "free",
19
+ role: "user",
20
+ isAdmin: false,
21
+ analyses_this_month: 0,
22
+ });
23
+ }
24
+
25
+ const { data: profile } = await supabase
26
+ .from("profiles")
27
+ .select("plan, role, is_banned, analyses_this_month, full_name, email")
28
+ .eq("id", user.id)
29
+ .single();
30
+
31
+ const plan = profile?.plan || "free";
32
+ const role = profile?.role || "user";
33
+
34
+ return NextResponse.json({
35
+ authenticated: true,
36
+ id: user.id,
37
+ email: profile?.email || user.email,
38
+ full_name: profile?.full_name || "",
39
+ plan,
40
+ role,
41
+ isAdmin: role === "admin",
42
+ is_banned: profile?.is_banned || false,
43
+ analyses_this_month: profile?.analyses_this_month || 0,
44
+ // Admins get unlimited everything
45
+ scan_limit: role === "admin" ? Infinity : plan === "free" ? 10 : Infinity,
46
+ can_upload: role === "admin" || plan !== "free",
47
+ can_compare: role === "admin" || plan !== "free",
48
+ can_export_pdf: role === "admin" || plan !== "free",
49
+ });
50
+ } catch (error) {
51
+ return NextResponse.json({
52
+ authenticated: false,
53
+ plan: "free",
54
+ role: "user",
55
+ isAdmin: false,
56
+ analyses_this_month: 0,
57
+ });
58
+ }
59
+ }