34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
// init.jsp SessionManager.hasSession 대응 - 인증 미들웨어
|
|
export function middleware(request: NextRequest) {
|
|
const { pathname } = request.nextUrl;
|
|
|
|
// 인증 불필요 경로
|
|
const publicPaths = ["/login", "/api/auth/login", "/_next", "/favicon.ico", "/icon.svg"];
|
|
if (publicPaths.some((p) => pathname.startsWith(p))) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
// 정적 자산(이미지/폰트 등)은 인증 면제
|
|
if (/\.(svg|png|jpg|jpeg|gif|webp|ico|woff2?|ttf|otf|css|js|map)$/i.test(pathname)) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
// 세션 쿠키 확인
|
|
const sessionCookie = request.cookies.get("plm-session");
|
|
if (!sessionCookie && !pathname.startsWith("/api")) {
|
|
return NextResponse.redirect(new URL("/login", request.url));
|
|
}
|
|
|
|
if (!sessionCookie && pathname.startsWith("/api")) {
|
|
return NextResponse.json({ success: false, message: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
|
|
};
|