mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:56:28 +00:00
1b37ff65f5
Gate administration actions by role permissions, centralize cached session access for data fetching, and return forbidden responses for unauthorized pages. Also lazy-load user exports and adjust search field sizing.
86 lines
2.7 KiB
TypeScript
86 lines
2.7 KiB
TypeScript
// "use server" opens a door from the client to the server, Exports must be async Server Functions. use for Form submissions, mutations, client-triggered server actions
|
|
// But import "server-only" locks the client out of a module, cannot call by client components. use for Database, auth, secret API calls
|
|
|
|
import "server-only";
|
|
import { cache } from "react";
|
|
import { getServerSession, type Session } from "next-auth";
|
|
import { ResponseData } from "@/types/types";
|
|
import { forbidden, redirect } from "next/navigation";
|
|
import { authOptions } from "./auth";
|
|
|
|
|
|
export const getCacheServerSession = cache(async (): Promise<Session | null> => {
|
|
return getServerSession(authOptions)
|
|
})
|
|
|
|
|
|
export const getAccessData = cache(async () => {
|
|
const session = await getCacheServerSession()
|
|
|
|
// Validate session first
|
|
if (!session?.user?.detail?.role?.id) {
|
|
redirect("/auth?error=Session%20error%20or%20has%20expired");
|
|
}
|
|
|
|
const res = await fetch(`${process.env.BASE_URL_API}/role/${session?.user.detail.role.id}`,
|
|
{
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${session?.user.access_token}`,
|
|
},
|
|
cache: "no-store",
|
|
}
|
|
);
|
|
|
|
if (!res.ok) {
|
|
if (res.status === 401 || res.status === 403) return redirect(`/auth?error=Session error or has expired !`)
|
|
// This will activate the closest `error.js` Error Boundar`)
|
|
throw new Error(`Failed to retrieve access data (${res.status}) ${res.statusText}`);
|
|
}
|
|
|
|
const data: ResponseData = await res.json()
|
|
|
|
return data.data
|
|
});
|
|
|
|
|
|
export async function getAccessPagePermission(path: string, action: string = "view") {
|
|
const accessData = await getAccessData();
|
|
|
|
// Check whether the current page is registered
|
|
const menu = accessData?.role_detail?.find((item: any) => item.menu?.path_url === path);
|
|
|
|
if (!menu?.menu) {
|
|
// redirect(redirectPage);
|
|
forbidden()
|
|
}
|
|
|
|
// Check whether the user has permission
|
|
if (!menu?.menu?.action?.includes(action.toUpperCase())) {
|
|
// redirect(redirectPage);
|
|
forbidden()
|
|
}
|
|
|
|
return menu?.menu?.action;
|
|
}
|
|
|
|
|
|
export async function getNonAdminAccessPagePermission(path: string, action: string = "view") {
|
|
const session = await getCacheServerSession()
|
|
|
|
if (!session?.user?.detail?.role?.role_detail) {
|
|
redirect("/auth?error=Session%20error%20or%20has%20expired");
|
|
}
|
|
|
|
const menu = session.user.detail?.role?.role_detail?.find(item => item.menu.path_url === path)
|
|
|
|
if (!menu) {
|
|
forbidden()
|
|
}
|
|
|
|
if (!menu.action?.includes(action.toUpperCase())) {
|
|
forbidden()
|
|
}
|
|
|
|
return menu.action
|
|
} |