Sudah fix menunya, superadmin bisa memilihkan menu untuk user

This commit is contained in:
2025-11-25 16:21:15 +08:00
parent ace5aff1b6
commit e941ed3893
6 changed files with 47 additions and 15 deletions

BIN
bun.lockb

Binary file not shown.

View File

@@ -54,6 +54,7 @@
"chart.js": "^4.4.8",
"classnames": "^2.5.1",
"colors": "^1.4.0",
"date-fns": "^4.1.0",
"dayjs": "^1.11.13",
"dotenv": "^17.2.3",
"elysia": "^1.3.5",

View File

@@ -138,7 +138,7 @@ export default function Validasi() {
});
cleanupStorage();
router.replace('/waiting-room');
window.location.href = '/waiting-room';
};
// ✅ Verifikasi OTP untuk LOGIN
@@ -167,7 +167,7 @@ export default function Validasi() {
cleanupStorage();
if (!isActive) {
router.replace('/waiting-room');
window.location.href = '/waiting-room';
return;
}

View File

@@ -1,19 +1,30 @@
// src/app/admin/(dashboard)/user&role/_com/dynamicNavbar.ts
import { devBar, navBar, role1, role2, role3 } from '@/app/admin/_com/list_PageAdmin';
// ✅ Helper: normalisasi ID menu agar konsisten
const normalizeMenuId = (id: string): string => {
return id.trim().toLowerCase();
};
export function getNavbar({
roleId,
menuIds,
}: {
roleId: number; // pastikan number
menuIds?: string[] | null; // opsional
roleId: number;
menuIds?: string[] | null;
}) {
// Prioritas: menuIds > roleId
// ✅ Jika menuIds tersedia, gunakan untuk filter — dengan normalisasi
if (menuIds && menuIds.length > 0) {
return navBar.filter(section => menuIds.includes(section.id));
// Normalisasi semua menuIds dari DB/state
const normalizedMenuSet = new Set(menuIds.map(id => normalizeMenuId(id)));
return navBar.filter(section => {
const normalizedSectionId = normalizeMenuId(section.id);
return normalizedMenuSet.has(normalizedSectionId);
});
}
// Fallback ke role-based
// 🔁 Fallback ke role-based navigation
if (roleId === 0) return devBar;
if (roleId === 1) return navBar;
if (roleId === 2) return role1;

View File

@@ -40,7 +40,7 @@ export default async function userUpdate(context: Context) {
...(isActive !== undefined && { isActive }),
...(roleId && { roleId }),
// Force logout: invalidate semua sesi
...(isRoleChanged || isActiveChanged ? { sessionInvalid: true } : {}),
...(isRoleChanged ? { sessionInvalid: true } : {}),
},
select: {
id: true,
@@ -53,7 +53,7 @@ export default async function userUpdate(context: Context) {
});
// ✅ HAPUS SEMUA SESI USER DI DATABASE
if (isRoleChanged || isActiveChanged) {
if (isRoleChanged) {
await prisma.userSession.deleteMany({ where: { userId: id } });
}

View File

@@ -1,6 +1,8 @@
// app/api/auth/_lib/session_create.ts
import { cookies } from "next/headers";
import { encrypt } from "./encrypt";
import prisma from "@/lib/prisma";
import { add } from "date-fns";
export async function sessionCreate({
sessionKey,
@@ -11,7 +13,7 @@ export async function sessionCreate({
sessionKey: string;
exp?: string;
jwtSecret: string;
user: Record<string, unknown>;
user: Record<string, unknown> & { id: string };
}) {
// ✅ Validasi env vars
if (!sessionKey || sessionKey.length === 0) {
@@ -26,17 +28,35 @@ export async function sessionCreate({
throw new Error("Token generation failed");
}
// Set cookie
// ✅ Hitung expiresAt sesuai exp
let expiresAt = add(new Date(), { days: 30 });
if (exp === "7 day") expiresAt = add(new Date(), { days: 7 });
// tambahkan opsi lain jika perlu
// Sebelum create session baru, nonaktifkan session aktif sebelumnya
await prisma.userSession.updateMany({
where: { userId: user.id, active: true },
data: { active: false },
});
// ✅ Simpan ke database
await prisma.userSession.create({
data: {
token,
userId: user.id,
active: true,
expiresAt,
},
});
// ✅ Set cookie
(await cookies()).set(sessionKey, token, {
httpOnly: true,
sameSite: "lax",
path: "/",
secure: process.env.NODE_ENV === "production",
maxAge: 30 * 24 * 60 * 60,
maxAge: 30 * 24 * 60 * 60, // seconds
});
console.log("✅ BASE_SESSION_KEY loaded:", !!process.env.BASE_SESSION_KEY);
console.log("✅ BASE_TOKEN_KEY loaded:", !!process.env.BASE_TOKEN_KEY);
return token;
}