duckil_plm/fems-app/src/middleware.tsx

56 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-11-02 18:01:31 +09:00
// src/middleware.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
2024-11-15 15:26:23 +09:00
// import { decodeToken, hasPermission, hasAnyPermission } from "@/lib/jwt";
import { decodeToken, hasPermission } from "@/lib/jwt";
2024-11-02 18:01:31 +09:00
2024-11-18 15:42:30 +09:00
// 인증이 필요없는 public 라우트 정의
const PUBLIC_ROUTES = ["/", "/login"];
2024-11-02 18:01:31 +09:00
export function middleware(request: NextRequest) {
const token = request.cookies.get("token")?.value;
2024-11-18 15:42:30 +09:00
const { pathname } = request.nextUrl;
// Public routes 체크
if (PUBLIC_ROUTES.includes(pathname)) {
2024-11-09 08:58:47 +09:00
return NextResponse.next();
}
2024-11-26 19:40:03 +09:00
// 비인증 사용자는 홈 페이지로
2024-11-09 08:58:47 +09:00
if (!token) {
2024-11-04 17:51:38 +09:00
return NextResponse.redirect(new URL("/", request.url));
2024-11-02 18:01:31 +09:00
}
2024-11-09 08:58:47 +09:00
// 토큰 디코딩
const tokenData = decodeToken(token);
if (!tokenData) {
return NextResponse.redirect(new URL("/", request.url));
}
// 권한 기반 라우팅 보호
2024-11-02 18:01:31 +09:00
if (request.nextUrl.pathname.startsWith("/admin")) {
2024-11-09 08:58:47 +09:00
// admin 페이지 접근을 위해서는 admin:access 권한이나 특정 role이 필요
const hasAdminAccess =
hasPermission(tokenData, "admin:access") ||
["super_admin", "company_admin"].includes(tokenData.role);
if (!hasAdminAccess) {
return NextResponse.redirect(new URL("/dashboard/overview", request.url));
}
}
// 응답 헤더에 사용자 권한 정보 추가 (옵션)
const response = NextResponse.next();
response.headers.set(
"X-User-Permissions",
JSON.stringify(tokenData.permissions)
);
response.headers.set("X-User-Role", tokenData.role);
return response;
2024-11-02 18:01:31 +09:00
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};