Fix: middleware

Deskripsi
- Fix middleware
- Fix metode login ( sekarang menggunakan api )
This commit is contained in:
2024-12-02 16:24:03 +08:00
parent 45279cd37f
commit 31124c5500
55 changed files with 1675 additions and 420 deletions

View File

@@ -1,3 +1,4 @@
import { sessionCreate } from "@/app/auth/_lib/session_create";
import prisma from "@/app/lib/prisma";
import { ServerEnv } from "@/app/lib/server_env";
import { sealData } from "iron-session";
@@ -7,45 +8,100 @@ import { NextResponse } from "next/server";
export async function POST(req: Request) {
if (req.method === "POST") {
const body = await req.json();
const { nomor } = await req.json();
const data = await prisma.user.findUnique({
const dataUser = await prisma.user.findUnique({
where: {
nomor: body.nomor,
nomor: nomor,
},
select: {
id: true,
nomor: true,
username: true,
active: true,
masterUserRoleId: true,
},
});
if (!data) return NextResponse.json({ status: 404 });
if (data) {
const res = await sealData(
JSON.stringify({
id: data.id,
username: data.username,
}),
{
password: ServerEnv.value?.WIBU_PWD as string,
}
if (dataUser === null)
return new Response(
JSON.stringify({ success: false, message: "Nomor Belum Terdaftar" }),
{ status: 404 }
);
cookies().set({
name: "mySession",
value: res,
maxAge: 60 * 60 * 24 * 7,
const token = await sessionCreate({
sessionKey: process.env.NEXT_PUBLIC_BASE_SESSION_KEY!,
encodedKey: process.env.NEXT_PUBLIC_BASE_TOKEN_KEY!,
user: dataUser as any,
});
const cekSessionUser = await prisma.userSession.findFirst({
where: {
userId: dataUser.id,
},
});
if (cekSessionUser !== null) {
await prisma.userSession.delete({
where: {
userId: dataUser.id,
},
});
revalidatePath("/dev/home");
return NextResponse.json({ status: 200, data });
}
return NextResponse.json({ success: true });
try {
const createUserSession = await prisma.userSession.create({
data: {
token: token as string,
userId: dataUser.id,
},
});
if (!createUserSession)
return new Response(
JSON.stringify({ success: false, message: "Gagal Membuat Session" }),
{ status: 400 }
);
} catch (error) {
console.log(error);
}
// if (data) {
// const res = await sealData(
// JSON.stringify({
// id: data.id,
// username: data.username,
// }),
// {
// password: ServerEnv.value?.WIBU_PWD as string,
// }
// );
// cookies().set({
// name: "mySession",
// value: res,
// maxAge: 60 * 60 * 24 * 7,
// });
// revalidatePath("/dev/home");
// return NextResponse.json({ status: 200, data });
// }
// return NextResponse.json({ success: true });
return new Response(
JSON.stringify({
success: true,
message: "Berhasil Login",
roleId: dataUser.masterUserRoleId,
active: dataUser.active,
}),
{ status: 200 }
);
}
return NextResponse.json({ success: false });
return new Response(
JSON.stringify({ success: false, message: "Method Not Allowed" }),
{ status: 405 }
);
}