feat: update the permission for showing the usage of the user

This commit is contained in:
2026-07-11 17:24:27 +07:00
parent 1e8204ebab
commit 40606ce39f
15 changed files with 399 additions and 27 deletions
+4 -1
View File
@@ -1,10 +1,12 @@
import { NextResponse } from "next/server";
import { getChartData } from "@/lib/usageDb";
import { requireUsageDashboardUser } from "@/lib/auth/currentUser";
const VALID_PERIODS = new Set(["today", "24h", "7d", "30d", "60d"]);
export async function GET(request) {
try {
const user = await requireUsageDashboardUser();
const { searchParams } = new URL(request.url);
const period = searchParams.get("period") || "7d";
@@ -12,9 +14,10 @@ export async function GET(request) {
return NextResponse.json({ error: "Invalid period" }, { status: 400 });
}
const data = await getChartData(period);
const data = await getChartData(period, user);
return NextResponse.json(data);
} catch (error) {
if (error?.message === "Unauthorized") return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
console.error("[API] Failed to get chart data:", error);
return NextResponse.json({ error: "Failed to fetch chart data" }, { status: 500 });
}