gaurv007 commited on
Commit
8ab19de
·
verified ·
1 Parent(s): 84ab7a2

Admin panel: dashboard (stats), user management (search/plan change/ban), scan viewer (delete), team manager, API key viewer (revoke), activity logs — admin: ankygaur9972@gmail.com

Browse files
web/app/admin/page.tsx ADDED
@@ -0,0 +1,334 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ "use client";
2
+
3
+ import { useState, useEffect } from "react";
4
+ import Link from "next/link";
5
+ import {
6
+ LayoutDashboard, Users, ScanText, Key, Shield, ScrollText, Activity,
7
+ Search, Ban, CircleCheck, ChevronDown, ChevronUp, Trash2, Crown,
8
+ ArrowLeft, RefreshCw, TriangleAlert, UserCheck, Eye, EyeOff
9
+ } from "lucide-react";
10
+
11
+ type Tab = "overview" | "users" | "scans" | "teams" | "api-keys" | "logs";
12
+
13
+ export default function AdminPage() {
14
+ const [tab, setTab] = useState<Tab>("overview");
15
+ const [stats, setStats] = useState<any>(null);
16
+ const [users, setUsers] = useState<any[]>([]);
17
+ const [scans, setScans] = useState<any[]>([]);
18
+ const [teams, setTeams] = useState<any[]>([]);
19
+ const [apiKeys, setApiKeys] = useState<any[]>([]);
20
+ const [logs, setLogs] = useState<any[]>([]);
21
+ const [search, setSearch] = useState("");
22
+ const [loading, setLoading] = useState(false);
23
+ const [userTotal, setUserTotal] = useState(0);
24
+ const [scanTotal, setScanTotal] = useState(0);
25
+
26
+ async function fetchData(action: string, params?: Record<string, string>) {
27
+ const qs = new URLSearchParams({ action, ...params });
28
+ const res = await fetch(`/api/admin?${qs}`);
29
+ if (!res.ok) { window.location.href = "/dashboard-pages/dashboard"; return null; }
30
+ return res.json();
31
+ }
32
+
33
+ async function adminAction(body: any) {
34
+ setLoading(true);
35
+ await fetch("/api/admin", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) });
36
+ setLoading(false);
37
+ loadTab(tab);
38
+ }
39
+
40
+ async function loadTab(t: Tab) {
41
+ setLoading(true);
42
+ switch (t) {
43
+ case "overview": { const d = await fetchData("stats"); if (d) setStats(d); break; }
44
+ case "users": { const d = await fetchData("users", search ? { search } : {}); if (d) { setUsers(d.users); setUserTotal(d.total); } break; }
45
+ case "scans": { const d = await fetchData("scans"); if (d) { setScans(d.scans); setScanTotal(d.total); } break; }
46
+ case "teams": { const d = await fetchData("teams"); if (d) setTeams(d.teams); break; }
47
+ case "api-keys": { const d = await fetchData("api-keys"); if (d) setApiKeys(d.keys); break; }
48
+ case "logs": { const d = await fetchData("logs"); if (d) setLogs(d.logs); break; }
49
+ }
50
+ setLoading(false);
51
+ }
52
+
53
+ useEffect(() => { loadTab(tab); }, [tab]);
54
+
55
+ const TABS: { key: Tab; label: string; icon: any }[] = [
56
+ { key: "overview", label: "Overview", icon: LayoutDashboard },
57
+ { key: "users", label: "Users", icon: Users },
58
+ { key: "scans", label: "Scans", icon: ScanText },
59
+ { key: "teams", label: "Teams", icon: Shield },
60
+ { key: "api-keys", label: "API Keys", icon: Key },
61
+ { key: "logs", label: "Activity", icon: Activity },
62
+ ];
63
+
64
+ const planBadge = (plan: string) => {
65
+ const c: Record<string, string> = { free: "bg-zinc-100 text-zinc-600", pro: "bg-blue-50 text-blue-700", team: "bg-purple-50 text-purple-700" };
66
+ return <span className={`text-[11px] font-medium px-2 py-0.5 rounded ${c[plan] || c.free}`}>{plan}</span>;
67
+ };
68
+
69
+ return (
70
+ <div className="min-h-screen bg-white">
71
+ <div className="max-w-6xl mx-auto px-5 py-8">
72
+ {/* Header */}
73
+ <div className="flex items-center justify-between mb-8">
74
+ <div>
75
+ <Link href="/dashboard-pages/dashboard" className="inline-flex items-center gap-1.5 text-sm text-zinc-400 hover:text-zinc-600 mb-2">
76
+ <ArrowLeft className="w-3.5 h-3.5" /> Dashboard
77
+ </Link>
78
+ <h1 className="text-2xl font-semibold tracking-tight flex items-center gap-2">
79
+ <Crown className="w-6 h-6 text-amber-500" />
80
+ Admin Panel
81
+ </h1>
82
+ </div>
83
+ <button onClick={() => loadTab(tab)} disabled={loading}
84
+ className="p-2 rounded-lg hover:bg-zinc-100 text-zinc-400 disabled:opacity-40">
85
+ <RefreshCw className={`w-4 h-4 ${loading ? "animate-spin" : ""}`} />
86
+ </button>
87
+ </div>
88
+
89
+ {/* Tabs */}
90
+ <div className="flex gap-1 mb-6 overflow-x-auto pb-1">
91
+ {TABS.map((t) => (
92
+ <button key={t.key} onClick={() => setTab(t.key)}
93
+ className={`flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-medium whitespace-nowrap transition-colors ${
94
+ tab === t.key ? "bg-zinc-900 text-white" : "text-zinc-500 hover:bg-zinc-100"
95
+ }`}>
96
+ <t.icon className="w-4 h-4" />
97
+ {t.label}
98
+ </button>
99
+ ))}
100
+ </div>
101
+
102
+ {/* Overview */}
103
+ {tab === "overview" && stats && (
104
+ <div className="space-y-6">
105
+ <div className="grid grid-cols-2 md:grid-cols-4 gap-4">
106
+ {[
107
+ { label: "Total Users", value: stats.total_users, sub: `${stats.banned_users} banned` },
108
+ { label: "Pro Users", value: stats.pro_users },
109
+ { label: "Team Users", value: stats.team_users },
110
+ { label: "Free Users", value: stats.free_users },
111
+ { label: "Total Scans", value: stats.total_scans },
112
+ { label: "Scans Today", value: stats.scans_today },
113
+ { label: "Teams", value: stats.total_teams },
114
+ { label: "Active API Keys", value: stats.total_api_keys },
115
+ ].map((s, i) => (
116
+ <div key={i} className="border border-zinc-200 rounded-xl p-4">
117
+ <p className="text-xs text-zinc-400">{s.label}</p>
118
+ <p className="text-2xl font-semibold mt-1">{s.value}</p>
119
+ {s.sub && <p className="text-xs text-zinc-400 mt-0.5">{s.sub}</p>}
120
+ </div>
121
+ ))}
122
+ </div>
123
+ </div>
124
+ )}
125
+
126
+ {/* Users */}
127
+ {tab === "users" && (
128
+ <div>
129
+ <div className="flex gap-2 mb-4">
130
+ <div className="flex-1 relative">
131
+ <Search className="w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-zinc-400" />
132
+ <input value={search} onChange={(e) => setSearch(e.target.value)}
133
+ onKeyDown={(e) => e.key === "Enter" && loadTab("users")}
134
+ placeholder="Search by email or name..."
135
+ className="w-full pl-10 pr-4 py-2.5 border border-zinc-200 rounded-lg text-sm focus:outline-none focus:border-zinc-400" />
136
+ </div>
137
+ <button onClick={() => loadTab("users")} className="px-4 bg-zinc-900 text-white rounded-lg text-sm font-medium hover:bg-zinc-800">Search</button>
138
+ </div>
139
+ <p className="text-xs text-zinc-400 mb-3">{userTotal} users total</p>
140
+ <div className="border border-zinc-200 rounded-xl overflow-hidden">
141
+ <table className="w-full text-sm">
142
+ <thead className="bg-zinc-50 border-b border-zinc-200">
143
+ <tr>
144
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">User</th>
145
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Plan</th>
146
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Scans</th>
147
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Joined</th>
148
+ <th className="text-right px-4 py-3 font-medium text-zinc-500">Actions</th>
149
+ </tr>
150
+ </thead>
151
+ <tbody className="divide-y divide-zinc-100">
152
+ {users.map((u) => (
153
+ <tr key={u.id} className={`${u.is_banned ? "opacity-50 bg-red-50/30" : "hover:bg-zinc-50"}`}>
154
+ <td className="px-4 py-3">
155
+ <p className="font-medium flex items-center gap-1.5">
156
+ {u.full_name || u.email}
157
+ {u.role === "admin" && <Crown className="w-3.5 h-3.5 text-amber-500" />}
158
+ {u.is_banned && <Ban className="w-3.5 h-3.5 text-red-500" />}
159
+ </p>
160
+ <p className="text-xs text-zinc-400">{u.email}</p>
161
+ </td>
162
+ <td className="px-4 py-3">{planBadge(u.plan)}</td>
163
+ <td className="px-4 py-3 text-zinc-500">{u.analyses_this_month}/mo</td>
164
+ <td className="px-4 py-3 text-xs text-zinc-400">{new Date(u.created_at).toLocaleDateString()}</td>
165
+ <td className="px-4 py-3 text-right">
166
+ <div className="flex gap-1 justify-end">
167
+ <select defaultValue={u.plan} onChange={(e) => adminAction({ action: "update_plan", userId: u.id, plan: e.target.value })}
168
+ className="text-xs border border-zinc-200 rounded px-2 py-1 bg-white">
169
+ <option value="free">Free</option>
170
+ <option value="pro">Pro</option>
171
+ <option value="team">Team</option>
172
+ </select>
173
+ <button onClick={() => adminAction({ action: "ban_user", userId: u.id, banned: !u.is_banned })}
174
+ className={`p-1.5 rounded hover:bg-zinc-100 ${u.is_banned ? "text-emerald-500" : "text-red-400"}`}
175
+ title={u.is_banned ? "Unban" : "Ban"}>
176
+ {u.is_banned ? <CircleCheck className="w-3.5 h-3.5" /> : <Ban className="w-3.5 h-3.5" />}
177
+ </button>
178
+ </div>
179
+ </td>
180
+ </tr>
181
+ ))}
182
+ </tbody>
183
+ </table>
184
+ </div>
185
+ </div>
186
+ )}
187
+
188
+ {/* Scans */}
189
+ {tab === "scans" && (
190
+ <div>
191
+ <p className="text-xs text-zinc-400 mb-3">{scanTotal} scans total</p>
192
+ <div className="border border-zinc-200 rounded-xl overflow-hidden">
193
+ <table className="w-full text-sm">
194
+ <thead className="bg-zinc-50 border-b border-zinc-200">
195
+ <tr>
196
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Source</th>
197
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Grade</th>
198
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Risk</th>
199
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Clauses</th>
200
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Date</th>
201
+ <th className="text-right px-4 py-3 font-medium text-zinc-500">Actions</th>
202
+ </tr>
203
+ </thead>
204
+ <tbody className="divide-y divide-zinc-100">
205
+ {scans.map((s) => (
206
+ <tr key={s.id} className="hover:bg-zinc-50">
207
+ <td className="px-4 py-3 max-w-xs truncate text-zinc-700">{s.source_url || "Manual scan"}</td>
208
+ <td className="px-4 py-3">
209
+ <span className={`text-xs font-semibold px-2 py-0.5 rounded ${
210
+ s.grade === "F" || s.grade === "D" ? "bg-red-50 text-red-700" :
211
+ s.grade === "C" ? "bg-amber-50 text-amber-700" : "bg-emerald-50 text-emerald-700"
212
+ }`}>{s.grade}</span>
213
+ </td>
214
+ <td className="px-4 py-3 text-zinc-500">{s.risk_score}/100</td>
215
+ <td className="px-4 py-3 text-zinc-500">{s.flagged_count}/{s.total_clauses}</td>
216
+ <td className="px-4 py-3 text-xs text-zinc-400">{new Date(s.created_at).toLocaleDateString()}</td>
217
+ <td className="px-4 py-3 text-right">
218
+ <button onClick={() => adminAction({ action: "delete_scan", scanId: s.id })}
219
+ className="p-1.5 rounded hover:bg-red-50 text-red-400" title="Delete">
220
+ <Trash2 className="w-3.5 h-3.5" />
221
+ </button>
222
+ </td>
223
+ </tr>
224
+ ))}
225
+ </tbody>
226
+ </table>
227
+ </div>
228
+ </div>
229
+ )}
230
+
231
+ {/* Teams */}
232
+ {tab === "teams" && (
233
+ <div className="border border-zinc-200 rounded-xl overflow-hidden">
234
+ <table className="w-full text-sm">
235
+ <thead className="bg-zinc-50 border-b border-zinc-200">
236
+ <tr>
237
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Team</th>
238
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Owner</th>
239
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Seats</th>
240
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Created</th>
241
+ <th className="text-right px-4 py-3 font-medium text-zinc-500">Actions</th>
242
+ </tr>
243
+ </thead>
244
+ <tbody className="divide-y divide-zinc-100">
245
+ {teams.map((t) => (
246
+ <tr key={t.id} className="hover:bg-zinc-50">
247
+ <td className="px-4 py-3 font-medium">{t.name}</td>
248
+ <td className="px-4 py-3 text-xs text-zinc-400 font-mono">{t.owner_id.slice(0, 8)}...</td>
249
+ <td className="px-4 py-3 text-zinc-500">{t.max_seats}</td>
250
+ <td className="px-4 py-3 text-xs text-zinc-400">{new Date(t.created_at).toLocaleDateString()}</td>
251
+ <td className="px-4 py-3 text-right">
252
+ <button onClick={() => adminAction({ action: "delete_team", teamId: t.id })}
253
+ className="p-1.5 rounded hover:bg-red-50 text-red-400" title="Delete team">
254
+ <Trash2 className="w-3.5 h-3.5" />
255
+ </button>
256
+ </td>
257
+ </tr>
258
+ ))}
259
+ {teams.length === 0 && <tr><td colSpan={5} className="px-4 py-8 text-center text-zinc-400">No teams yet.</td></tr>}
260
+ </tbody>
261
+ </table>
262
+ </div>
263
+ )}
264
+
265
+ {/* API Keys */}
266
+ {tab === "api-keys" && (
267
+ <div className="border border-zinc-200 rounded-xl overflow-hidden">
268
+ <table className="w-full text-sm">
269
+ <thead className="bg-zinc-50 border-b border-zinc-200">
270
+ <tr>
271
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Name</th>
272
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Key</th>
273
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Usage</th>
274
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Status</th>
275
+ <th className="text-right px-4 py-3 font-medium text-zinc-500">Actions</th>
276
+ </tr>
277
+ </thead>
278
+ <tbody className="divide-y divide-zinc-100">
279
+ {apiKeys.map((k) => (
280
+ <tr key={k.id} className="hover:bg-zinc-50">
281
+ <td className="px-4 py-3 font-medium">{k.name}</td>
282
+ <td className="px-4 py-3 text-xs text-zinc-400 font-mono">{k.key_prefix}</td>
283
+ <td className="px-4 py-3 text-zinc-500">{k.calls_this_month}/{k.calls_limit}</td>
284
+ <td className="px-4 py-3">
285
+ <span className={`text-xs font-medium px-2 py-0.5 rounded ${k.is_active ? "bg-emerald-50 text-emerald-700" : "bg-red-50 text-red-700"}`}>
286
+ {k.is_active ? "Active" : "Revoked"}
287
+ </span>
288
+ </td>
289
+ <td className="px-4 py-3 text-right">
290
+ {k.is_active && (
291
+ <button onClick={() => adminAction({ action: "revoke_api_key", keyId: k.id })}
292
+ className="p-1.5 rounded hover:bg-red-50 text-red-400" title="Revoke">
293
+ <Ban className="w-3.5 h-3.5" />
294
+ </button>
295
+ )}
296
+ </td>
297
+ </tr>
298
+ ))}
299
+ {apiKeys.length === 0 && <tr><td colSpan={5} className="px-4 py-8 text-center text-zinc-400">No API keys.</td></tr>}
300
+ </tbody>
301
+ </table>
302
+ </div>
303
+ )}
304
+
305
+ {/* Activity Logs */}
306
+ {tab === "logs" && (
307
+ <div className="border border-zinc-200 rounded-xl overflow-hidden">
308
+ <table className="w-full text-sm">
309
+ <thead className="bg-zinc-50 border-b border-zinc-200">
310
+ <tr>
311
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Action</th>
312
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Target</th>
313
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Details</th>
314
+ <th className="text-left px-4 py-3 font-medium text-zinc-500">Time</th>
315
+ </tr>
316
+ </thead>
317
+ <tbody className="divide-y divide-zinc-100">
318
+ {logs.map((l) => (
319
+ <tr key={l.id} className="hover:bg-zinc-50">
320
+ <td className="px-4 py-3 font-medium">{l.action}</td>
321
+ <td className="px-4 py-3 text-xs text-zinc-400">{l.target_type}: {l.target_id?.slice(0, 8)}...</td>
322
+ <td className="px-4 py-3 text-xs text-zinc-400 font-mono">{l.details ? JSON.stringify(l.details) : "—"}</td>
323
+ <td className="px-4 py-3 text-xs text-zinc-400">{new Date(l.created_at).toLocaleString()}</td>
324
+ </tr>
325
+ ))}
326
+ {logs.length === 0 && <tr><td colSpan={4} className="px-4 py-8 text-center text-zinc-400">No admin actions yet.</td></tr>}
327
+ </tbody>
328
+ </table>
329
+ </div>
330
+ )}
331
+ </div>
332
+ </div>
333
+ );
334
+ }
web/app/api/admin/route.ts ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
15
+ // GET — admin stats + data
16
+ export async function GET(req: NextRequest) {
17
+ const { supabase, user, error } = await checkAdmin();
18
+ if (error || !supabase || !user) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
19
+
20
+ const url = new URL(req.url);
21
+ const action = url.searchParams.get("action");
22
+
23
+ switch (action) {
24
+ case "stats": {
25
+ const { count: totalUsers } = await supabase.from("profiles").select("id", { count: "exact", head: true });
26
+ const { count: proUsers } = await supabase.from("profiles").select("id", { count: "exact", head: true }).eq("plan", "pro");
27
+ const { count: teamUsers } = await supabase.from("profiles").select("id", { count: "exact", head: true }).eq("plan", "team");
28
+ const { count: totalScans } = await supabase.from("analyses").select("id", { count: "exact", head: true });
29
+ const { count: totalTeams } = await supabase.from("teams").select("id", { count: "exact", head: true });
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());
37
+
38
+ return NextResponse.json({
39
+ total_users: totalUsers || 0,
40
+ pro_users: proUsers || 0,
41
+ team_users: teamUsers || 0,
42
+ free_users: (totalUsers || 0) - (proUsers || 0) - (teamUsers || 0),
43
+ total_scans: totalScans || 0,
44
+ scans_today: scansToday || 0,
45
+ total_teams: totalTeams || 0,
46
+ total_api_keys: totalApiKeys || 0,
47
+ banned_users: bannedUsers || 0,
48
+ });
49
+ }
50
+
51
+ case "users": {
52
+ const limit = parseInt(url.searchParams.get("limit") || "50");
53
+ const offset = parseInt(url.searchParams.get("offset") || "0");
54
+ const search = url.searchParams.get("search") || "";
55
+
56
+ let query = supabase.from("profiles")
57
+ .select("id, email, full_name, plan, role, is_banned, analyses_this_month, team_id, razorpay_subscription_id, created_at", { count: "exact" })
58
+ .order("created_at", { ascending: false })
59
+ .range(offset, offset + limit - 1);
60
+
61
+ if (search) {
62
+ query = query.or(`email.ilike.%${search}%,full_name.ilike.%${search}%`);
63
+ }
64
+
65
+ const { data: users, count } = await query;
66
+ return NextResponse.json({ users: users || [], total: count || 0 });
67
+ }
68
+
69
+ case "scans": {
70
+ const limit = parseInt(url.searchParams.get("limit") || "50");
71
+ const offset = parseInt(url.searchParams.get("offset") || "0");
72
+
73
+ const { data: scans, count } = await supabase.from("analyses")
74
+ .select("id, user_id, source_url, risk_score, grade, flagged_count, total_clauses, created_at", { count: "exact" })
75
+ .order("created_at", { ascending: false })
76
+ .range(offset, offset + limit - 1);
77
+
78
+ return NextResponse.json({ scans: scans || [], total: count || 0 });
79
+ }
80
+
81
+ case "teams": {
82
+ const { data: teams } = await supabase.from("teams").select("*").order("created_at", { ascending: false });
83
+ return NextResponse.json({ teams: teams || [] });
84
+ }
85
+
86
+ case "api-keys": {
87
+ const { data: keys } = await supabase.from("api_keys")
88
+ .select("id, user_id, name, key_prefix, calls_this_month, calls_limit, is_active, created_at")
89
+ .order("created_at", { ascending: false });
90
+ return NextResponse.json({ keys: keys || [] });
91
+ }
92
+
93
+ case "logs": {
94
+ const { data: logs } = await supabase.from("admin_logs")
95
+ .select("*").order("created_at", { ascending: false }).limit(100);
96
+ return NextResponse.json({ logs: logs || [] });
97
+ }
98
+
99
+ default:
100
+ return NextResponse.json({ error: "Invalid action" }, { status: 400 });
101
+ }
102
+ }
103
+
104
+ // POST — admin actions
105
+ export async function POST(req: NextRequest) {
106
+ const { supabase, user, error } = await checkAdmin();
107
+ if (error || !supabase || !user) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
108
+
109
+ const body = await req.json();
110
+
111
+ switch (body.action) {
112
+ case "update_plan": {
113
+ const { userId, plan } = body;
114
+ await supabase.from("profiles").update({ plan, updated_at: new Date().toISOString() }).eq("id", userId);
115
+ await supabase.from("admin_logs").insert({
116
+ admin_id: user.id, action: "update_plan", target_type: "user", target_id: userId,
117
+ details: { plan },
118
+ });
119
+ return NextResponse.json({ success: true });
120
+ }
121
+
122
+ case "ban_user": {
123
+ const { userId, banned } = body;
124
+ await supabase.from("profiles").update({ is_banned: banned, updated_at: new Date().toISOString() }).eq("id", userId);
125
+ await supabase.from("admin_logs").insert({
126
+ admin_id: user.id, action: banned ? "ban_user" : "unban_user", target_type: "user", target_id: userId,
127
+ });
128
+ return NextResponse.json({ success: true });
129
+ }
130
+
131
+ case "delete_scan": {
132
+ const { scanId } = body;
133
+ await supabase.from("analyses").delete().eq("id", scanId);
134
+ await supabase.from("admin_logs").insert({
135
+ admin_id: user.id, action: "delete_scan", target_type: "scan", target_id: scanId,
136
+ });
137
+ return NextResponse.json({ success: true });
138
+ }
139
+
140
+ case "revoke_api_key": {
141
+ const { keyId } = body;
142
+ await supabase.from("api_keys").update({ is_active: false }).eq("id", keyId);
143
+ await supabase.from("admin_logs").insert({
144
+ admin_id: user.id, action: "revoke_api_key", target_type: "api_key", target_id: keyId,
145
+ });
146
+ return NextResponse.json({ success: true });
147
+ }
148
+
149
+ case "delete_team": {
150
+ const { teamId } = body;
151
+ await supabase.from("profiles").update({ team_id: null }).eq("team_id", teamId);
152
+ await supabase.from("teams").delete().eq("id", teamId);
153
+ await supabase.from("admin_logs").insert({
154
+ admin_id: user.id, action: "delete_team", target_type: "team", target_id: teamId,
155
+ });
156
+ return NextResponse.json({ success: true });
157
+ }
158
+
159
+ default:
160
+ return NextResponse.json({ error: "Invalid action" }, { status: 400 });
161
+ }
162
+ }
web/components/nav.tsx CHANGED
@@ -2,8 +2,9 @@
2
 
3
  import Link from "next/link";
4
  import { usePathname } from "next/navigation";
5
- import { ShieldCheck, Menu, X } from "lucide-react";
6
- import { useState } from "react";
 
7
 
8
  const links = [
9
  { href: "/#features", label: "Features" },
@@ -11,10 +12,21 @@ const links = [
11
  { href: "/dashboard-pages/analyze", label: "Scanner" },
12
  ];
13
 
 
 
14
  export function Nav() {
15
  const [open, setOpen] = useState(false);
 
16
  const pathname = usePathname();
17
  const isDashboard = pathname?.startsWith("/dashboard");
 
 
 
 
 
 
 
 
18
 
19
  return (
20
  <nav className="sticky top-0 z-50 bg-white/80 backdrop-blur-md border-b border-zinc-100">
@@ -24,7 +36,6 @@ export function Nav() {
24
  <span className="font-semibold text-[15px] tracking-tight text-zinc-900">ClauseGuard</span>
25
  </Link>
26
 
27
- {/* Desktop */}
28
  <div className="hidden md:flex items-center gap-1">
29
  {links.map((l) => (
30
  <a key={l.href} href={l.href}
@@ -32,8 +43,17 @@ export function Nav() {
32
  {l.label}
33
  </a>
34
  ))}
 
 
 
 
 
 
 
 
35
  <div className="w-px h-4 bg-zinc-200 mx-2" />
36
- {isDashboard ? (
 
37
  <Link href="/dashboard-pages/settings"
38
  className="px-3 py-1.5 text-[13px] text-zinc-500 hover:text-zinc-900 rounded-md hover:bg-zinc-50">
39
  Settings
@@ -44,27 +64,27 @@ export function Nav() {
44
  Log in
45
  </Link>
46
  )}
47
- <Link href={isDashboard ? "/dashboard-pages/analyze" : "/auth/signup"}
48
  className="ml-1 px-3.5 py-1.5 text-[13px] font-medium text-white bg-zinc-900 rounded-md hover:bg-zinc-800 transition-colors">
49
- {isDashboard ? "New scan" : "Get started"}
50
  </Link>
51
  </div>
52
 
53
- {/* Mobile toggle */}
54
  <button className="md:hidden p-1.5 rounded-md hover:bg-zinc-100" onClick={() => setOpen(!open)}>
55
  {open ? <X className="w-5 h-5" /> : <Menu className="w-5 h-5" />}
56
  </button>
57
  </div>
58
 
59
- {/* Mobile menu */}
60
  {open && (
61
  <div className="md:hidden border-t border-zinc-100 bg-white px-5 py-3 space-y-1">
62
  {links.map((l) => (
63
  <a key={l.href} href={l.href} onClick={() => setOpen(false)}
64
- className="block px-3 py-2 text-sm text-zinc-600 rounded-md hover:bg-zinc-50">
65
- {l.label}
66
- </a>
67
  ))}
 
 
 
 
68
  <Link href="/auth/login" onClick={() => setOpen(false)}
69
  className="block px-3 py-2 text-sm text-zinc-600 rounded-md hover:bg-zinc-50">Log in</Link>
70
  </div>
 
2
 
3
  import Link from "next/link";
4
  import { usePathname } from "next/navigation";
5
+ import { ShieldCheck, Menu, X, Crown } from "lucide-react";
6
+ import { useState, useEffect } from "react";
7
+ import { createClient } from "@/lib/supabase/client";
8
 
9
  const links = [
10
  { href: "/#features", label: "Features" },
 
12
  { href: "/dashboard-pages/analyze", label: "Scanner" },
13
  ];
14
 
15
+ const ADMIN_EMAILS = ["ankygaur9972@gmail.com"];
16
+
17
  export function Nav() {
18
  const [open, setOpen] = useState(false);
19
+ const [userEmail, setUserEmail] = useState<string | null>(null);
20
  const pathname = usePathname();
21
  const isDashboard = pathname?.startsWith("/dashboard");
22
+ const isAdmin = userEmail && ADMIN_EMAILS.includes(userEmail);
23
+
24
+ useEffect(() => {
25
+ const supabase = createClient();
26
+ supabase.auth.getUser().then(({ data }) => {
27
+ setUserEmail(data.user?.email || null);
28
+ });
29
+ }, []);
30
 
31
  return (
32
  <nav className="sticky top-0 z-50 bg-white/80 backdrop-blur-md border-b border-zinc-100">
 
36
  <span className="font-semibold text-[15px] tracking-tight text-zinc-900">ClauseGuard</span>
37
  </Link>
38
 
 
39
  <div className="hidden md:flex items-center gap-1">
40
  {links.map((l) => (
41
  <a key={l.href} href={l.href}
 
43
  {l.label}
44
  </a>
45
  ))}
46
+
47
+ {isAdmin && (
48
+ <Link href="/admin"
49
+ className="px-3 py-1.5 text-[13px] text-amber-600 hover:text-amber-700 rounded-md hover:bg-amber-50 transition-colors flex items-center gap-1">
50
+ <Crown className="w-3.5 h-3.5" /> Admin
51
+ </Link>
52
+ )}
53
+
54
  <div className="w-px h-4 bg-zinc-200 mx-2" />
55
+
56
+ {isDashboard || userEmail ? (
57
  <Link href="/dashboard-pages/settings"
58
  className="px-3 py-1.5 text-[13px] text-zinc-500 hover:text-zinc-900 rounded-md hover:bg-zinc-50">
59
  Settings
 
64
  Log in
65
  </Link>
66
  )}
67
+ <Link href={isDashboard || userEmail ? "/dashboard-pages/analyze" : "/auth/signup"}
68
  className="ml-1 px-3.5 py-1.5 text-[13px] font-medium text-white bg-zinc-900 rounded-md hover:bg-zinc-800 transition-colors">
69
+ {isDashboard || userEmail ? "New scan" : "Get started"}
70
  </Link>
71
  </div>
72
 
 
73
  <button className="md:hidden p-1.5 rounded-md hover:bg-zinc-100" onClick={() => setOpen(!open)}>
74
  {open ? <X className="w-5 h-5" /> : <Menu className="w-5 h-5" />}
75
  </button>
76
  </div>
77
 
 
78
  {open && (
79
  <div className="md:hidden border-t border-zinc-100 bg-white px-5 py-3 space-y-1">
80
  {links.map((l) => (
81
  <a key={l.href} href={l.href} onClick={() => setOpen(false)}
82
+ className="block px-3 py-2 text-sm text-zinc-600 rounded-md hover:bg-zinc-50">{l.label}</a>
 
 
83
  ))}
84
+ {isAdmin && (
85
+ <Link href="/admin" onClick={() => setOpen(false)}
86
+ className="block px-3 py-2 text-sm text-amber-600 rounded-md hover:bg-amber-50">Admin</Link>
87
+ )}
88
  <Link href="/auth/login" onClick={() => setOpen(false)}
89
  className="block px-3 py-2 text-sm text-zinc-600 rounded-md hover:bg-zinc-50">Log in</Link>
90
  </div>
web/lib/admin-guard.ts ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { createClient } from "@/lib/supabase/server";
2
+ import { redirect } from "next/navigation";
3
+
4
+ const ADMIN_EMAILS = ["ankygaur9972@gmail.com"];
5
+
6
+ export async function requireAdmin() {
7
+ const supabase = await createClient();
8
+ const { data: { user } } = await supabase.auth.getUser();
9
+
10
+ if (!user) redirect("/auth/login");
11
+
12
+ // Check email first (fast)
13
+ if (!ADMIN_EMAILS.includes(user.email || "")) {
14
+ redirect("/dashboard-pages/dashboard");
15
+ }
16
+
17
+ // Double check role in DB
18
+ const { data: profile } = await supabase
19
+ .from("profiles")
20
+ .select("role")
21
+ .eq("id", user.id)
22
+ .single();
23
+
24
+ if (profile?.role !== "admin") {
25
+ redirect("/dashboard-pages/dashboard");
26
+ }
27
+
28
+ return { user, supabase };
29
+ }
web/lib/supabase/admin-schema.sql ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -- ClauseGuard — Admin Schema Extension
2
+ -- Run AFTER the main schema.sql
3
+
4
+ -- Add role column to profiles
5
+ ALTER TABLE public.profiles ADD COLUMN IF NOT EXISTS role TEXT DEFAULT 'user' CHECK (role IN ('user', 'admin'));
6
+
7
+ -- Add is_banned column
8
+ ALTER TABLE public.profiles ADD COLUMN IF NOT EXISTS is_banned BOOLEAN DEFAULT false;
9
+
10
+ -- Set admin
11
+ UPDATE public.profiles SET role = 'admin' WHERE email = 'ankygaur9972@gmail.com';
12
+
13
+ -- Activity log
14
+ CREATE TABLE IF NOT EXISTS public.admin_logs (
15
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
16
+ admin_id UUID REFERENCES auth.users NOT NULL,
17
+ action TEXT NOT NULL,
18
+ target_type TEXT, -- 'user', 'team', 'scan', 'api_key', 'rule'
19
+ target_id TEXT,
20
+ details JSONB,
21
+ created_at TIMESTAMPTZ DEFAULT NOW()
22
+ );
23
+
24
+ CREATE INDEX IF NOT EXISTS idx_admin_logs_created ON public.admin_logs(created_at DESC);
25
+
26
+ -- Admin RLS: admins can read all tables
27
+ CREATE POLICY "Admins can read all profiles" ON public.profiles FOR SELECT
28
+ USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
29
+
30
+ CREATE POLICY "Admins can update all profiles" ON public.profiles FOR UPDATE
31
+ USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
32
+
33
+ CREATE POLICY "Admins can read all analyses" ON public.analyses FOR SELECT
34
+ USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
35
+
36
+ CREATE POLICY "Admins can read all teams" ON public.teams FOR SELECT
37
+ USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
38
+
39
+ CREATE POLICY "Admins can read all api_keys" ON public.api_keys FOR SELECT
40
+ USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
41
+
42
+ CREATE POLICY "Admins can read all custom_rules" ON public.custom_rules FOR SELECT
43
+ USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
44
+
45
+ ALTER TABLE public.admin_logs ENABLE ROW LEVEL SECURITY;
46
+ CREATE POLICY "Admins can manage logs" ON public.admin_logs FOR ALL
47
+ USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));