fix: update the permission for create and read the api keys

This commit is contained in:
2026-07-11 15:28:52 +07:00
parent 8cd10aefdc
commit 4ac342c5a2
15 changed files with 187 additions and 72 deletions
+12 -3
View File
@@ -1,5 +1,6 @@
import { NextResponse } from "next/server";
import { getApiKeys, createApiKey } from "@/lib/localDb";
import { getApiKeysByOwnerId, createApiKey } from "@/lib/localDb";
import { requireCurrentDashboardUser } from "@/lib/auth/currentUser";
import { getConsistentMachineId } from "@/shared/utils/machineId";
export const dynamic = "force-dynamic";
@@ -7,9 +8,13 @@ export const dynamic = "force-dynamic";
// GET /api/keys - List API keys
export async function GET() {
try {
const keys = await getApiKeys();
const user = await requireCurrentDashboardUser();
const keys = await getApiKeysByOwnerId(user.id);
return NextResponse.json({ keys });
} catch (error) {
if (error.message === "Unauthorized") {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
console.log("Error fetching keys:", error);
return NextResponse.json({ error: "Failed to fetch keys" }, { status: 500 });
}
@@ -18,6 +23,7 @@ export async function GET() {
// POST /api/keys - Create new API key
export async function POST(request) {
try {
const user = await requireCurrentDashboardUser();
const body = await request.json();
const { name } = body;
@@ -27,7 +33,7 @@ export async function POST(request) {
// Always get machineId from server
const machineId = await getConsistentMachineId();
const apiKey = await createApiKey(name, machineId);
const apiKey = await createApiKey(name, machineId, user.id);
return NextResponse.json({
key: apiKey.key,
@@ -36,6 +42,9 @@ export async function POST(request) {
machineId: apiKey.machineId,
}, { status: 201 });
} catch (error) {
if (error.message === "Unauthorized") {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
console.log("Error creating key:", error);
return NextResponse.json({ error: "Failed to create key" }, { status: 500 });
}