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
@@ -6,6 +6,7 @@ import { getUsageForProvider } from "open-sse/services/usage.js";
import { getExecutor } from "open-sse/executors/index.js";
import { resolveConnectionProxyConfig } from "@/lib/network/connectionProxy";
import { USAGE_APIKEY_PROVIDERS } from "@/shared/constants/providers";
import { requireUsageDashboardUser } from "@/lib/auth/currentUser";
// Detect auth-expired messages returned by usage providers instead of throwing
const AUTH_EXPIRED_PATTERNS = ["expired", "authentication", "unauthorized", "401", "re-authorize"];
@@ -166,9 +167,10 @@ export async function GET(request, { params }) {
}, { status: 401 });
}
}
const user = await requireUsageDashboardUser();
// Fetch usage from provider API
let usage = await getUsageForProvider(connection, proxyOptions);
connection = await getProviderConnectionById(connectionId, user.role === "admin" ? null : user.id);
// If provider returned an auth-expired message instead of throwing,
// force-refresh token and retry once (OAuth only)
@@ -184,6 +186,7 @@ export async function GET(request, { params }) {
return Response.json(usage);
} catch (error) {
if (error?.message === "Unauthorized") return Response.json({ error: "Unauthorized" }, { status: 401 });
const provider = connection?.provider ?? "unknown";
console.warn(`[Usage] ${provider}: ${error.message}`);
return Response.json({ error: error.message }, { status: 500 });