Fix auth
This commit is contained in:
@@ -14,6 +14,7 @@ export async function GET(request: NextRequest) {
|
|||||||
id: id as string,
|
id: id as string,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return NextResponse.json(data, { status: 200 });
|
return NextResponse.json(data, { status: 200 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { prisma } from "@/app/lib";
|
|
||||||
import { cookies } from "next/headers";
|
import { cookies } from "next/headers";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
@@ -7,15 +6,15 @@ export async function GET(request: NextRequest) {
|
|||||||
// const { searchParams } = new URL(request.url);
|
// const { searchParams } = new URL(request.url);
|
||||||
// const id = searchParams.get("id");
|
// const id = searchParams.get("id");
|
||||||
|
|
||||||
const delToken = await prisma.userSession.delete({
|
// const delToken = await prisma.userSession.delete({
|
||||||
where: {
|
// where: {
|
||||||
userId: id as string,
|
// userId: id as string,
|
||||||
},
|
// },
|
||||||
});
|
// });
|
||||||
|
|
||||||
const del = cookies().delete(process.env.NEXT_PUBLIC_BASE_SESSION_KEY!);
|
const del = cookies().delete(process.env.NEXT_PUBLIC_BASE_SESSION_KEY!);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ success: true, message: "Logout Berhasil" },
|
{ success: true, message: "Logout Berhasil" },
|
||||||
{ status: 200 }
|
{ status: 200 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,73 +6,76 @@ export async function POST(req: Request) {
|
|||||||
if (req.method === "POST") {
|
if (req.method === "POST") {
|
||||||
const { nomor } = await req.json();
|
const { nomor } = await req.json();
|
||||||
|
|
||||||
const dataUser = await prisma.user.findUnique({
|
|
||||||
where: {
|
|
||||||
nomor: nomor,
|
|
||||||
},
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
nomor: true,
|
|
||||||
username: true,
|
|
||||||
active: true,
|
|
||||||
masterUserRoleId: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (dataUser === null)
|
|
||||||
return NextResponse.json(
|
|
||||||
{ success: false, message: "Nomor Belum Terdaftar" },
|
|
||||||
{ status: 404 }
|
|
||||||
);
|
|
||||||
|
|
||||||
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,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const createUserSession = await prisma.userSession.create({
|
const dataUser = await prisma.user.findUnique({
|
||||||
data: {
|
where: {
|
||||||
token: token as string,
|
nomor: nomor,
|
||||||
userId: dataUser.id,
|
},
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
nomor: true,
|
||||||
|
username: true,
|
||||||
|
active: true,
|
||||||
|
masterUserRoleId: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!createUserSession)
|
if (dataUser === null)
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ success: false, message: "Gagal Membuat Session" },
|
{ success: false, message: "Nomor Belum Terdaftar" },
|
||||||
{ status: 400 }
|
{ status: 404 }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
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,
|
||||||
|
// },
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
|
||||||
|
// try {
|
||||||
|
// const createUserSession = await prisma.userSession.create({
|
||||||
|
// data: {
|
||||||
|
// token: token as string,
|
||||||
|
// userId: dataUser.id,
|
||||||
|
// },
|
||||||
|
// });
|
||||||
|
|
||||||
|
// if (!createUserSession)
|
||||||
|
// return NextResponse.json(
|
||||||
|
// { success: false, message: "Gagal Membuat Session" },
|
||||||
|
// { status: 400 }
|
||||||
|
// );
|
||||||
|
// } catch (error) {
|
||||||
|
// console.log(error);
|
||||||
|
// }
|
||||||
|
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
success: true,
|
||||||
|
message: "Berhasil Login",
|
||||||
|
roleId: dataUser.masterUserRoleId,
|
||||||
|
active: dataUser.active,
|
||||||
|
},
|
||||||
|
{ status: 200 }
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
success: true,
|
|
||||||
message: "Berhasil Login",
|
|
||||||
roleId: dataUser.masterUserRoleId,
|
|
||||||
active: dataUser.active,
|
|
||||||
},
|
|
||||||
{ status: 200 }
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ success: false, message: "Method Not Allowed" },
|
{ success: false, message: "Method Not Allowed" },
|
||||||
{ status: 405 }
|
{ status: 405 }
|
||||||
|
|||||||
@@ -70,8 +70,8 @@ export default function Validasi() {
|
|||||||
const result = await res.json();
|
const result = await res.json();
|
||||||
|
|
||||||
onSetData({
|
onSetData({
|
||||||
nomor: result.data.nomor,
|
nomor: result.nomor,
|
||||||
code: result.data.otp,
|
code: result.otp,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user