mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
52 lines
1.7 KiB
JavaScript
52 lines
1.7 KiB
JavaScript
import { NextResponse } from "next/server";
|
|
import { getApiKeysByOwnerId, createApiKey } from "@/lib/localDb";
|
|
import { requireCurrentDashboardUser } from "@/lib/auth/currentUser";
|
|
import { getConsistentMachineId } from "@/shared/utils/machineId";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
// GET /api/keys - List API keys
|
|
export async function GET() {
|
|
try {
|
|
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 });
|
|
}
|
|
}
|
|
|
|
// 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;
|
|
|
|
if (!name) {
|
|
return NextResponse.json({ error: "Name is required" }, { status: 400 });
|
|
}
|
|
|
|
// Always get machineId from server
|
|
const machineId = await getConsistentMachineId();
|
|
const apiKey = await createApiKey(name, machineId, user.id);
|
|
|
|
return NextResponse.json({
|
|
key: apiKey.key,
|
|
name: apiKey.name,
|
|
id: apiKey.id,
|
|
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 });
|
|
}
|
|
}
|