fix error server

This commit is contained in:
2025-02-10 12:15:43 +08:00
parent 7cafd9ff79
commit 075d480969
13 changed files with 177 additions and 160 deletions

View File

@@ -3,42 +3,46 @@ import backendLogger from "@/util/backendLogger";
import { NextResponse } from "next/server";
export async function GET(request: Request) {
const method = request.method;
if (method !== "GET") {
return NextResponse.json({
success: false,
message: "Method not allowed",
if (request.method !== "GET") {
return NextResponse.json(
{
success: false,
message: "Method not allowed",
},
{ status: 405 }
);
}
try {
let fixData;
fixData = await prisma.voting.count({
where: {
Voting_Status: {
name: "Publish",
},
{ status: 405 }
)
}
try {
let fixData;
fixData = await prisma.voting.count({
where: {
Voting_Status: {
name: "Publish",
},
isArsip: true,
}
})
return NextResponse.json({
success: true,
message: 'Success get data voting dashboard',
data: fixData
},
{ status: 200 }
)
} catch (error) {
backendLogger.error('Error get data voting dashboard >>', error);
NextResponse.json({
success: false,
message: 'Error get data voting dashboard',
reason: (error as Error).message
},
{ status: 500 }
)
} finally {
await prisma.$disconnect();
}
}
isArsip: true,
},
});
return NextResponse.json(
{
success: true,
message: "Success get data voting dashboard",
data: fixData,
},
{ status: 200 }
);
} catch (error) {
backendLogger.error("Error get data voting dashboard >>", error);
return NextResponse.json(
{
success: false,
message: "Error get data voting dashboard",
reason: (error as Error).message,
},
{ status: 500 }
);
} finally {
await prisma.$disconnect();
}
}

View File

@@ -1,20 +1,39 @@
import { decrypt } from "@/app/(auth)/_lib/decrypt";
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";
import { NextResponse } from "next/server";
export const dynamic = "force-dynamic";
export async function GET(request: NextRequest) {
const id = request.nextUrl.searchParams.get("id");
// const { searchParams } = new URL(request.url);
// const id = searchParams.get("id");
// const delToken = await prisma.userSession.delete({
// where: {
// userId: id as string,
// },
// });
export async function GET() {
const sessionKey = process.env.NEXT_PUBLIC_BASE_SESSION_KEY!; // Gunakan environment variable yang tidak diekspos ke client-side
if (!sessionKey) {
return NextResponse.json(
{ success: false, message: "Session key tidak ditemukan" },
{ status: 500 }
);
}
const del = cookies().delete(process.env.NEXT_PUBLIC_BASE_SESSION_KEY!);
return NextResponse.json(
{ success: true, message: "Logout Berhasil" },
{ status: 200 }
);
const cookieStore = cookies();
const sessionCookie = cookieStore.get(sessionKey);
if (!sessionCookie) {
return NextResponse.json(
{ success: false, message: "Session tidak ditemukan" },
{ status: 400 }
);
}
try {
cookieStore.delete(sessionKey);
return NextResponse.json(
{ success: true, message: "Logout berhasil" },
{ status: 200 }
);
} catch (error) {
console.error("Gagal menghapus cookie:", error);
return NextResponse.json(
{ success: false, message: "Gagal melakukan logout" },
{ status: 500 }
);
}
}

View File

@@ -38,7 +38,14 @@ export async function POST(req: Request) {
user: dataUser as any,
});
return NextResponse.json(
if (!token) {
return NextResponse.json(
{ success: false, message: "Gagal membuat session" },
{ status: 500 }
);
}
// Buat response dengan token dalam cookie
const response = NextResponse.json(
{
success: true,
message: "Berhasil Login",
@@ -47,6 +54,16 @@ export async function POST(req: Request) {
},
{ status: 200 }
);
// Set cookie dengan token yang sudah dipastikan tidak null
response.cookies.set(process.env.NEXT_PUBLIC_BASE_SESSION_KEY!, token, {
path: "/",
sameSite: "lax",
secure: process.env.NODE_ENV === "production",
maxAge: 30 * 24 * 60 * 60, // 30 hari dalam detik (1 bulan)
});
return response;
} catch (error) {
backendLogger.log("API Error or Server Error", error);
return NextResponse.json(

View File

@@ -21,13 +21,11 @@ export async function GET(request: Request) {
},
});
await prisma.$disconnect();
return NextResponse.json(
{ success: true, message: "Berhasil mendapatkan data", data: res },
{ status: 200 }
);
} catch (error) {
await prisma.$disconnect();
backendLogger.error("Error Get Master Bank >>", error);
return NextResponse.json(
{
@@ -37,5 +35,7 @@ export async function GET(request: Request) {
},
{ status: 500 }
);
} finally {
await prisma.$disconnect();
}
}