39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
||
import { db } from "@/db";
|
||
import { apiKeys } from "@/db/schema";
|
||
import { and, eq } from "drizzle-orm";
|
||
import { authenticateWebOrAPIRequest } from "@/app/lib/api-auth";
|
||
|
||
/**
|
||
* DELETE /api/v1/api-keys/[id] — Kendi anahtarını iptal et (isActive: false)
|
||
*/
|
||
export async function DELETE(
|
||
request: NextRequest,
|
||
context: { params: Promise<{ id: string }> }
|
||
) {
|
||
const auth = await authenticateWebOrAPIRequest(request);
|
||
if (!auth.authenticated || !auth.userId) {
|
||
return NextResponse.json({ error: auth.error ?? "Yetkisiz" }, { status: 401 });
|
||
}
|
||
|
||
const { id } = await context.params;
|
||
|
||
const updated = await db
|
||
.update(apiKeys)
|
||
.set({ isActive: false, updatedAt: new Date() })
|
||
.where(and(eq(apiKeys.id, id), eq(apiKeys.userId, auth.userId)))
|
||
.returning({ id: apiKeys.id });
|
||
|
||
if (updated.length === 0) {
|
||
return NextResponse.json(
|
||
{ error: "Anahtar bulunamadı veya size ait değil." },
|
||
{ status: 404 }
|
||
);
|
||
}
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
message: "API anahtarı iptal edildi.",
|
||
});
|
||
}
|