fix middleware

This commit is contained in:
2025-02-05 15:07:18 +08:00
parent 331b5c9a84
commit 6dc4e7afc3
10 changed files with 713 additions and 203 deletions

View File

@@ -1,8 +1,12 @@
import { prisma } from "@/app/lib";
import backendLogger from "@/util/backendLogger";
import { NextResponse } from "next/server";
export { DELETE };
async function DELETE(request: Request) {
async function DELETE(
request: Request,
{ params }: { params: { id: string } }
) {
if (request.method !== "DELETE") {
return NextResponse.json(
{ success: false, message: "Method not allowed" },
@@ -10,41 +14,24 @@ async function DELETE(request: Request) {
);
}
try {
// Ambil parameter nomor dari URL
const { nomor } = await request.json();
// Ambil parameter id dari URL
const { id } = params;
// Validasi parameter nomor
if (!nomor) {
if (!id) {
return NextResponse.json(
{
success: false,
message: "Parameter 'nomor' diperlukan",
message: "Parameter 'id' diperlukan",
},
{ status: 400 }
);
}
// Cek apakah data OTP dengan nomor tersebut ada
const existingOtp = await prisma.kodeOtp.findFirst({
orderBy: {
createdAt: "desc",
},
where: { nomor },
});
if (!existingOtp) {
return NextResponse.json(
{
success: false,
message: "Data OTP tidak ditemukan",
},
{ status: 404 }
);
}
// Hapus data OTP
await prisma.kodeOtp.deleteMany({
where: { nomor },
await prisma.kodeOtp.delete({
where: {
id: id,
},
});
return NextResponse.json(
@@ -55,7 +42,7 @@ async function DELETE(request: Request) {
{ status: 200 }
);
} catch (error) {
console.error("Error deleting OTP:", error);
backendLogger.error("Error deleting OTP:", error);
return NextResponse.json(
{
success: false,

View File

@@ -32,7 +32,6 @@ export async function GET(req: NextRequest) {
});
// Disconnect after successful query
await prisma.$disconnect();
return NextResponse.json({
success: true,
@@ -41,7 +40,6 @@ export async function GET(req: NextRequest) {
});
} catch (error) {
// Ensure connection is closed even if error occurs
await prisma.$disconnect();
console.error("Error in user validation:", error);
return NextResponse.json(
@@ -51,5 +49,7 @@ export async function GET(req: NextRequest) {
},
{ status: 500 }
);
} finally {
await prisma.$disconnect();
}
}

View File

@@ -1,10 +1,9 @@
import { NextResponse } from "next/server";
export async function GET(req: Request) {
const auth = req.headers.get("Authorization");
const token = auth?.split(" ")[1];
console.log("TOKEN>", token);
if (!token) return NextResponse.json({ success: false }, { status: 401 });
return NextResponse.json({ success: true });