feat: tambah dependensi 'jose' versi 5.9.2 pada package.json

refactor: rapikan identasi dan buat field 'expires' opsional di model UserSession pada schema prisma

chore: bersihkan import tidak terpakai di route login dan register API
This commit is contained in:
2024-09-18 14:39:39 +08:00
parent 2ccbca6566
commit 6ee43ed20f
83 changed files with 794 additions and 273 deletions

View File

@@ -1,18 +1,32 @@
"use server";
import prisma from "@/app/lib/prisma";
import { funGetUserIdByToken } from "@/app_modules/_global/fun/get";
import { revalidatePath } from "next/cache";
import { cookies } from "next/headers";
export async function auth_Logout(kodeId: string) {
cookies().set({
name: "ssn",
value: "",
maxAge: 0,
});
const c = cookies().get("ssn");
if (c?.value !== "") return { status: 400, message: "Gagal Logout" };
const userId = await funGetUserIdByToken();
try {
const delToken = await prisma.userSession.delete({
where: {
userId: userId,
},
});
if (!delToken) return { status: 400, message: "Gagal Hapus User Session" };
cookies().set({
name: "ssn",
value: "",
maxAge: 0,
expires: 0,
});
return { status: 200, message: "Logout Berhasil" };
} catch (error) {
console.log(error);
}
// const del = await prisma.kodeOtp.delete({
// where: {

View File

@@ -1,13 +1,16 @@
"use server";
import { PwdCookies } from "@/app/lib";
import prisma from "@/app/lib/prisma";
import { sealData } from "iron-session";
import { cookies } from "next/headers";
export async function Auth_funRegister(data: any) {
export async function Auth_funRegister({
data,
HIPMI_PWD,
}: {
data: any;
HIPMI_PWD: string;
}) {
const cekUsername = await prisma.user.findUnique({
where: {
username: data.username,
@@ -28,21 +31,31 @@ export async function Auth_funRegister(data: any) {
});
if (!create) return { status: 400, message: "Gagal Mendaftar" };
const seal = await sealData(
const sealToken = await sealData(
JSON.stringify({
id: create.id,
username: create.username,
}),
{
password: PwdCookies
password: HIPMI_PWD,
}
);
cookies().set({
name: "ssn",
value: seal,
maxAge: 60 * 60 * 24 * 7,
value: sealToken,
// maxAge: 60 * 60 * 24 * 7,
});
const createUserSession = await prisma.userSession.create({
data: {
token: sealToken,
userId: create.id,
},
});
if (!createUserSession)
return { status: 400, message: "Gagal Membuat User Session" };
return { status: 200, message: "Berhasil Mendaftar" };
}

View File

@@ -1,15 +1,19 @@
"use server";
import prisma from "@/app/lib/prisma";
import { sealData } from "iron-session";
import { cookies } from "next/headers";
import { revalidatePath } from "next/cache";
import { RouterHome } from "@/app/lib/router_hipmi/router_home";
import { PwdCookies } from "@/app/lib";
import { sealData, unsealData } from "iron-session";
import { revalidatePath } from "next/cache";
import { cookies } from "next/headers";
export async function auth_funValidasi(nomor: string) {
const cek = await prisma.user.findUnique({
export async function auth_funValidasi({
nomor,
HIPMI_PWD,
}: {
nomor: string;
HIPMI_PWD: string;
}) {
const cekUser = await prisma.user.findUnique({
where: {
nomor: nomor,
},
@@ -21,30 +25,44 @@ export async function auth_funValidasi(nomor: string) {
},
});
if (cek === null) return { status: 400, message: "Nomor Belum Terdaftar" };
if (cek) {
const res = await sealData(
JSON.stringify({
id: cek.id,
username: cek.username,
}),
{
password: PwdCookies,
}
);
if (cekUser === null) return { status: 400, message: "Nomor Belum Terdaftar" };
cookies().set({
name: "ssn",
value: res,
maxAge: 60 * 60 * 24 * 30,
const sealToken = await sealData(
JSON.stringify({
id: cekUser.id,
username: cekUser.username,
}),
{
password: HIPMI_PWD,
}
);
cookies().set({
name: "ssn",
value: sealToken,
// maxAge: 60 * 60 * 24 * 30,
// expires: 60 * 60 * 24 * 30,
});
try {
const createUserSession = await prisma.userSession.create({
data: {
token: sealToken,
userId: cekUser.id,
},
});
if (!createUserSession)
return { status: 401, message: "Gagal Membuat User Session" };
revalidatePath(RouterHome.main_home);
} catch (error) {
console.log(error);
}
return {
status: 200,
message: "Nomor Terverifikasi",
role: cek.masterUserRoleId,
role: cekUser.masterUserRoleId,
};
}