mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-06 07:12:12 +03:00
36
src/app/api/keys/[id]/regenerate/route.ts
Normal file
36
src/app/api/keys/[id]/regenerate/route.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { regenerateApiKey } from "@/lib/localDb";
|
||||
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
|
||||
import * as log from "@/sse/utils/logger";
|
||||
|
||||
/**
|
||||
* POST /api/keys/[id]/regenerate
|
||||
*
|
||||
* Regenerates the API key value for a given ID.
|
||||
* The old key is immediately invalidated.
|
||||
*/
|
||||
export async function POST(request, { params }) {
|
||||
const authError = await requireManagementAuth(request);
|
||||
if (authError) return authError;
|
||||
|
||||
try {
|
||||
const { id } = await params;
|
||||
if (!id) {
|
||||
return NextResponse.json({ error: "Missing key ID" }, { status: 400 });
|
||||
}
|
||||
|
||||
const result = await regenerateApiKey(id);
|
||||
if (!result) {
|
||||
return NextResponse.json({ error: "Key not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
message: "API key regenerated successfully",
|
||||
key: result.key,
|
||||
id: result.id,
|
||||
});
|
||||
} catch (error) {
|
||||
log.error("keys", "Error regenerating key", error);
|
||||
return NextResponse.json({ error: "Failed to regenerate key" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -70,8 +70,11 @@ export async function PATCH(request, { params }) {
|
||||
noLog,
|
||||
autoResolve,
|
||||
isActive,
|
||||
isBanned,
|
||||
expiresAt,
|
||||
maxSessions,
|
||||
accessSchedule,
|
||||
rateLimits,
|
||||
scopes,
|
||||
} = validation.data;
|
||||
|
||||
@@ -82,8 +85,11 @@ export async function PATCH(request, { params }) {
|
||||
if (noLog !== undefined) payload.noLog = noLog;
|
||||
if (autoResolve !== undefined) payload.autoResolve = autoResolve;
|
||||
if (isActive !== undefined) payload.isActive = isActive;
|
||||
if (isBanned !== undefined) payload.isBanned = isBanned;
|
||||
if (expiresAt !== undefined) payload.expiresAt = expiresAt;
|
||||
if (maxSessions !== undefined) payload.maxSessions = maxSessions;
|
||||
if (accessSchedule !== undefined) payload.accessSchedule = accessSchedule;
|
||||
if (rateLimits !== undefined) payload.rateLimits = rateLimits;
|
||||
if (scopes !== undefined) payload.scopes = scopes;
|
||||
|
||||
const updated = await updateApiKeyPermissions(id, payload);
|
||||
@@ -102,8 +108,11 @@ export async function PATCH(request, { params }) {
|
||||
...(noLog !== undefined && { noLog }),
|
||||
...(autoResolve !== undefined && { autoResolve }),
|
||||
...(isActive !== undefined && { isActive }),
|
||||
...(isBanned !== undefined && { isBanned }),
|
||||
...(expiresAt !== undefined && { expiresAt }),
|
||||
...(maxSessions !== undefined && { maxSessions }),
|
||||
...(accessSchedule !== undefined && { accessSchedule }),
|
||||
...(rateLimits !== undefined && { rateLimits }),
|
||||
...(scopes !== undefined && { scopes }),
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user