Sudah fix menunya, superadmin bisa memilihkan menu untuk user
This commit is contained in:
@@ -54,6 +54,7 @@
|
|||||||
"chart.js": "^4.4.8",
|
"chart.js": "^4.4.8",
|
||||||
"classnames": "^2.5.1",
|
"classnames": "^2.5.1",
|
||||||
"colors": "^1.4.0",
|
"colors": "^1.4.0",
|
||||||
|
"date-fns": "^4.1.0",
|
||||||
"dayjs": "^1.11.13",
|
"dayjs": "^1.11.13",
|
||||||
"dotenv": "^17.2.3",
|
"dotenv": "^17.2.3",
|
||||||
"elysia": "^1.3.5",
|
"elysia": "^1.3.5",
|
||||||
|
|||||||
@@ -138,7 +138,7 @@ export default function Validasi() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
cleanupStorage();
|
cleanupStorage();
|
||||||
router.replace('/waiting-room');
|
window.location.href = '/waiting-room';
|
||||||
};
|
};
|
||||||
|
|
||||||
// ✅ Verifikasi OTP untuk LOGIN
|
// ✅ Verifikasi OTP untuk LOGIN
|
||||||
@@ -167,7 +167,7 @@ export default function Validasi() {
|
|||||||
cleanupStorage();
|
cleanupStorage();
|
||||||
|
|
||||||
if (!isActive) {
|
if (!isActive) {
|
||||||
router.replace('/waiting-room');
|
window.location.href = '/waiting-room';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,19 +1,30 @@
|
|||||||
// src/app/admin/(dashboard)/user&role/_com/dynamicNavbar.ts
|
// src/app/admin/(dashboard)/user&role/_com/dynamicNavbar.ts
|
||||||
import { devBar, navBar, role1, role2, role3 } from '@/app/admin/_com/list_PageAdmin';
|
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({
|
export function getNavbar({
|
||||||
roleId,
|
roleId,
|
||||||
menuIds,
|
menuIds,
|
||||||
}: {
|
}: {
|
||||||
roleId: number; // pastikan number
|
roleId: number;
|
||||||
menuIds?: string[] | null; // opsional
|
menuIds?: string[] | null;
|
||||||
}) {
|
}) {
|
||||||
// Prioritas: menuIds > roleId
|
// ✅ Jika menuIds tersedia, gunakan untuk filter — dengan normalisasi
|
||||||
if (menuIds && menuIds.length > 0) {
|
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 === 0) return devBar;
|
||||||
if (roleId === 1) return navBar;
|
if (roleId === 1) return navBar;
|
||||||
if (roleId === 2) return role1;
|
if (roleId === 2) return role1;
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ export default async function userUpdate(context: Context) {
|
|||||||
...(isActive !== undefined && { isActive }),
|
...(isActive !== undefined && { isActive }),
|
||||||
...(roleId && { roleId }),
|
...(roleId && { roleId }),
|
||||||
// Force logout: invalidate semua sesi
|
// Force logout: invalidate semua sesi
|
||||||
...(isRoleChanged || isActiveChanged ? { sessionInvalid: true } : {}),
|
...(isRoleChanged ? { sessionInvalid: true } : {}),
|
||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
@@ -53,7 +53,7 @@ export default async function userUpdate(context: Context) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// ✅ HAPUS SEMUA SESI USER DI DATABASE
|
// ✅ HAPUS SEMUA SESI USER DI DATABASE
|
||||||
if (isRoleChanged || isActiveChanged) {
|
if (isRoleChanged) {
|
||||||
await prisma.userSession.deleteMany({ where: { userId: id } });
|
await prisma.userSession.deleteMany({ where: { userId: id } });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
// app/api/auth/_lib/session_create.ts
|
// app/api/auth/_lib/session_create.ts
|
||||||
import { cookies } from "next/headers";
|
import { cookies } from "next/headers";
|
||||||
import { encrypt } from "./encrypt";
|
import { encrypt } from "./encrypt";
|
||||||
|
import prisma from "@/lib/prisma";
|
||||||
|
import { add } from "date-fns";
|
||||||
|
|
||||||
export async function sessionCreate({
|
export async function sessionCreate({
|
||||||
sessionKey,
|
sessionKey,
|
||||||
@@ -11,7 +13,7 @@ export async function sessionCreate({
|
|||||||
sessionKey: string;
|
sessionKey: string;
|
||||||
exp?: string;
|
exp?: string;
|
||||||
jwtSecret: string;
|
jwtSecret: string;
|
||||||
user: Record<string, unknown>;
|
user: Record<string, unknown> & { id: string };
|
||||||
}) {
|
}) {
|
||||||
// ✅ Validasi env vars
|
// ✅ Validasi env vars
|
||||||
if (!sessionKey || sessionKey.length === 0) {
|
if (!sessionKey || sessionKey.length === 0) {
|
||||||
@@ -26,17 +28,35 @@ export async function sessionCreate({
|
|||||||
throw new Error("Token generation failed");
|
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, {
|
(await cookies()).set(sessionKey, token, {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
sameSite: "lax",
|
sameSite: "lax",
|
||||||
path: "/",
|
path: "/",
|
||||||
secure: process.env.NODE_ENV === "production",
|
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;
|
return token;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user