feat: แกไขสวน backend ใหเขากบ frontend

This commit is contained in:
admin
2025-09-27 11:30:31 +07:00
parent 4cb7801fe8
commit db7030883f
7 changed files with 693 additions and 333 deletions

View File

@@ -1,24 +1,34 @@
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
// ชื่อคุกกี้ให้ตรงกับ backend
const COOKIE_NAME = "access_token";
export function middleware(req: NextRequest) {
const protectedPaths = [
"/dashboard","/drawings","/rfas","/transmittals","/correspondences",
"/contracts-volumes","/users","/reports","/workflow","/health","/admin"
];
const { pathname } = req.nextUrl;
const isProtected = protectedPaths.some(p => pathname.startsWith(p));
if (!isProtected) return NextResponse.next();
const hasToken = req.cookies.get("access_token");
// ตรวจคุกกี้
const hastoken = req.cookies.get(COOKIE_NAME)?.value;
if (!hasToken) {
const url = req.nextUrl.clone();
url.pathname = "/login";
return NextResponse.redirect(url);
const loginUrl = new URL("/login", req.url);
// จำเส้นทางเดิมไว้เพื่อเด้งกลับหลังล็อกอิน
loginUrl.searchParams.set("from", pathname);
return NextResponse.redirect(loginUrl);
}
return NextResponse.next();
}
export const config = { matcher: [
"/dashboard/:path*","/drawings/:path*","/rfas/:path*","/transmittals/:path*","/correspondences/:path*",
"/contracts-volumes/:path*","/users/:path*","/reports/:path*","/workflow/:path*","/health/:path*","/admin/:path*"
] };
export const config = {
matcher: [
"/dashboard/:path*",
"/drawings/:path*",
"/rfas/:path*",
"/transmittals/:path*",
"/correspondences/:path*",
"/contracts-volumes/:path*",
"/users/:path*",
"/reports/:path*",
"/workflow/:path*",
"/admin/:path*",
],
};