mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +00:00
fix: update the permission for create and read the api keys
This commit is contained in:
@@ -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 });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user