ปรับ frontend
This commit is contained in:
@@ -1,69 +1,112 @@
|
||||
// app/layout.jsx
|
||||
import "./globals.css";
|
||||
import { Inter } from "next/font/google";
|
||||
// File: frontend/app/(protected)/layout.jsx
|
||||
import Link from "next/link";
|
||||
import { redirect } from "next/navigation";
|
||||
import { cookies, headers } from "next/headers";
|
||||
// ถ้ามี lib rbac เดิมอยู่ให้ใช้ต่อได้
|
||||
import { can } from "@/lib/rbac";
|
||||
|
||||
const inter = Inter({ subsets: ["latin"] });
|
||||
// แก้ title ให้ถูกสะกด
|
||||
export const metadata = { title: "DMS | Protected" };
|
||||
|
||||
export const metadata = {
|
||||
title: "DMS",
|
||||
description: "Document Management System",
|
||||
};
|
||||
const API_BASE = (process.env.NEXT_PUBLIC_API_BASE || "").replace(/\/$/, "");
|
||||
|
||||
async function fetchSessionFromAPI() {
|
||||
// ดึงคุกกี้จริงจากฝั่งเซิร์ฟเวอร์ แล้วส่งต่อให้ backend
|
||||
const cookieHeader = cookies().toString(); // serialize ทั้งชุด
|
||||
const hostHdr = headers().get("host");
|
||||
const protoHdr = headers().get("x-forwarded-proto") || "https";
|
||||
|
||||
const res = await fetch(`${API_BASE}/api/auth/me`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Cookie: cookieHeader,
|
||||
// เผื่อ backend ตรวจ origin/proto/host
|
||||
"X-Forwarded-Host": hostHdr || "",
|
||||
"X-Forwarded-Proto": protoHdr,
|
||||
Accept: "application/json",
|
||||
},
|
||||
// server component ไม่ต้องใช้ credentials
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
if (!res.ok) return null;
|
||||
try {
|
||||
const data = await res.json();
|
||||
return data?.ok ? data : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export default async function ProtectedLayout({ children }) {
|
||||
const session = await fetchSessionFromAPI();
|
||||
if (!session) {
|
||||
// พยายามส่ง next path กลับไปที่ /login
|
||||
redirect("/login?next=/dashboard");
|
||||
}
|
||||
const { user } = session;
|
||||
|
||||
export default function RootLayout({ children }) {
|
||||
return (
|
||||
<html lang="en" suppressHydrationWarning>
|
||||
<body
|
||||
className={`${inter.className} min-h-screen bg-background text-foreground`}
|
||||
>
|
||||
<div className="flex min-h-screen">
|
||||
{/* Sidebar */}
|
||||
<aside className="w-64 bg-primary text-primary-foreground p-4">
|
||||
<h1 className="text-xl font-bold mb-6">📂 DMS</h1>
|
||||
<nav className="space-y-2">
|
||||
<a
|
||||
href="/dashboard"
|
||||
className="block px-3 py-2 rounded hover:bg-accent"
|
||||
>
|
||||
Dashboard
|
||||
</a>
|
||||
<a
|
||||
href="/correspondences"
|
||||
className="block px-3 py-2 rounded hover:bg-accent"
|
||||
>
|
||||
Correspondences
|
||||
</a>
|
||||
<a
|
||||
href="/users"
|
||||
className="block px-3 py-2 rounded hover:bg-accent"
|
||||
>
|
||||
Users
|
||||
</a>
|
||||
<a
|
||||
href="/health"
|
||||
className="block px-3 py-2 rounded hover:bg-accent"
|
||||
>
|
||||
Health
|
||||
</a>
|
||||
</nav>
|
||||
</aside>
|
||||
<section className="grid grid-cols-12 gap-6 p-4 mx-auto max-w-7xl">
|
||||
<aside className="col-span-12 lg:col-span-3 xl:col-span-3">
|
||||
<div className="p-4 border rounded-3xl bg-white/70">
|
||||
<div className="mb-3 text-sm">RBAC: <b>{user.role}</b></div>
|
||||
|
||||
{/* Main content */}
|
||||
<main className="flex-1 flex flex-col">
|
||||
{/* Top Navbar */}
|
||||
<header className="h-14 bg-secondary text-secondary-foreground flex items-center justify-between px-6 shadow-sm">
|
||||
<span className="font-medium">Laem Chabang Port Phase 3</span>
|
||||
<div className="flex items-center space-x-4">
|
||||
<button className="px-3 py-1 rounded bg-accent text-accent-foreground hover:opacity-90">
|
||||
Quick Action
|
||||
</button>
|
||||
<span className="text-sm">superadmin</span>
|
||||
</div>
|
||||
</header>
|
||||
<nav className="space-y-2">
|
||||
<Link className="block px-4 py-2 rounded-xl bg-white/60 hover:bg-white" href="/dashboard">แดชบอร์ด</Link>
|
||||
<Link className="block px-4 py-2 rounded-xl bg-white/60 hover:bg-white" href="/drawings">Drawings</Link>
|
||||
<Link className="block px-4 py-2 rounded-xl bg-white/60 hover:bg-white" href="/rfas">RFAs</Link>
|
||||
<Link className="block px-4 py-2 rounded-xl bg-white/60 hover:bg-white" href="/transmittals">Transmittals</Link>
|
||||
<Link className="block px-4 py-2 rounded-xl bg-white/60 hover:bg-white" href="/correspondences">Correspondences</Link>
|
||||
<Link className="block px-4 py-2 rounded-xl bg-white/60 hover:bg-white" href="/contracts-volumes">Contracts & Volumes</Link>
|
||||
<Link className="block px-4 py-2 rounded-xl bg-white/60 hover:bg-white" href="/reports">Reports</Link>
|
||||
|
||||
<section className="p-6 flex-1">{children}</section>
|
||||
</main>
|
||||
{can(user, "workflow:view") && (
|
||||
<Link className="block px-4 py-2 rounded-xl bg-white/60 hover:bg-white" href="/workflow">Workflow (n8n)</Link>
|
||||
)}
|
||||
{can(user, "health:view") && (
|
||||
<Link className="block px-4 py-2 rounded-xl bg-white/60 hover:bg-white" href="/health">Health</Link>
|
||||
)}
|
||||
{can(user, "users:manage") && (
|
||||
<Link className="block px-4 py-2 rounded-xl bg-white/60 hover:bg-white" href="/users">ผู้ใช้/บทบาท</Link>
|
||||
)}
|
||||
</nav>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
</aside>
|
||||
|
||||
<main className="col-span-12 space-y-6 lg:col-span-9 xl:col-span-9">
|
||||
{/* System / Quick Actions */}
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="flex-1 text-lg font-semibold">Document Management System — LCBP3 Phase 3</div>
|
||||
|
||||
{can(user, "admin:view") && (
|
||||
<a className="px-3 py-2 text-white rounded-xl" style={{ background: "#0D5C75" }} href="/admin">Admin</a>
|
||||
)}
|
||||
{can(user, "users:manage") && (
|
||||
<a className="px-3 py-2 text-white rounded-xl" style={{ background: "#0D5C75" }} href="/users">ผู้ใช้/บทบาท</a>
|
||||
)}
|
||||
{can(user, "health:view") && (
|
||||
<a className="px-3 py-2 text-white rounded-xl" style={{ background: "#0D5C75" }} href="/health">Health</a>
|
||||
)}
|
||||
{can(user, "workflow:view") && (
|
||||
<a className="px-3 py-2 text-white rounded-xl" style={{ background: "#0D5C75" }} href="/workflow">Workflow</a>
|
||||
)}
|
||||
{can(user, "rfa:create") && (
|
||||
<a className="px-3 py-2 text-white rounded-xl" style={{ background: "#0D5C75" }} href="/rfas/new">+ RFA</a>
|
||||
)}
|
||||
{can(user, "drawing:upload") && (
|
||||
<a className="px-3 py-2 text-white rounded-xl" style={{ background: "#0D5C75" }} href="/drawings/upload">+ Upload Drawing</a>
|
||||
)}
|
||||
{can(user, "transmittal:create") && (
|
||||
<a className="px-3 py-2 text-white rounded-xl" style={{ background: "#0D5C75" }} href="/transmittals/new">+ Transmittal</a>
|
||||
)}
|
||||
{can(user, "correspondence:create") && (
|
||||
<a className="px-3 py-2 text-white rounded-xl" style={{ background: "#0D5C75" }} href="/correspondences/new">+ หนังสือสื่อสาร</a>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{children}
|
||||
</main>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user