gaurv007 commited on
Commit
f9d1091
·
verified ·
1 Parent(s): 3d6bc12

v3.0: Fix admin API route — remove hardcoded email, check DB profiles.role only

Browse files
Files changed (1) hide show
  1. web/app/api/admin/route.ts +12 -4
web/app/api/admin/route.ts CHANGED
@@ -1,14 +1,23 @@
1
  import { NextRequest, NextResponse } from "next/server";
2
  import { createClient } from "@/lib/supabase/server";
3
 
4
- const ADMIN_EMAILS = ["ankygaur9972@gmail.com"];
5
-
6
  async function checkAdmin() {
7
  const supabase = await createClient();
8
  const { data: { user } } = await supabase.auth.getUser();
9
- if (!user || !ADMIN_EMAILS.includes(user.email || "")) {
 
 
 
 
 
 
 
 
 
10
  return { supabase: null, user: null, error: true };
11
  }
 
12
  return { supabase, user, error: false };
13
  }
14
 
@@ -30,7 +39,6 @@ export async function GET(req: NextRequest) {
30
  const { count: totalApiKeys } = await supabase.from("api_keys").select("id", { count: "exact", head: true }).eq("is_active", true);
31
  const { count: bannedUsers } = await supabase.from("profiles").select("id", { count: "exact", head: true }).eq("is_banned", true);
32
 
33
- // Scans today
34
  const today = new Date();
35
  today.setHours(0, 0, 0, 0);
36
  const { count: scansToday } = await supabase.from("analyses").select("id", { count: "exact", head: true }).gte("created_at", today.toISOString());
 
1
  import { NextRequest, NextResponse } from "next/server";
2
  import { createClient } from "@/lib/supabase/server";
3
 
4
+ // No hardcoded emails — admin access is determined by profiles.role in the database
 
5
  async function checkAdmin() {
6
  const supabase = await createClient();
7
  const { data: { user } } = await supabase.auth.getUser();
8
+ if (!user) return { supabase: null, user: null, error: true };
9
+
10
+ // Check role from database
11
+ const { data: profile } = await supabase
12
+ .from("profiles")
13
+ .select("role")
14
+ .eq("id", user.id)
15
+ .single();
16
+
17
+ if (profile?.role !== "admin") {
18
  return { supabase: null, user: null, error: true };
19
  }
20
+
21
  return { supabase, user, error: false };
22
  }
23
 
 
39
  const { count: totalApiKeys } = await supabase.from("api_keys").select("id", { count: "exact", head: true }).eq("is_active", true);
40
  const { count: bannedUsers } = await supabase.from("profiles").select("id", { count: "exact", head: true }).eq("is_banned", true);
41
 
 
42
  const today = new Date();
43
  today.setHours(0, 0, 0, 0);
44
  const { count: scansToday } = await supabase.from("analyses").select("id", { count: "exact", head: true }).gte("created_at", today.toISOString());