Files
vexplor/frontend/middleware.ts
DDD1542 5afa373b1f refactor: Update middleware and enhance component interactions
- Improved the middleware to handle authentication checks more effectively, ensuring that users are redirected appropriately based on their authentication status.
- Updated the InteractiveScreenViewerDynamic and RealtimePreviewDynamic components to utilize a new subscription method for DOM manipulation during drag events, enhancing performance and user experience.
- Refactored the SplitLineComponent to optimize drag handling and state management, ensuring smoother interactions during component adjustments.
- Integrated API client for menu data loading, streamlining token management and error handling.
2026-02-24 11:02:43 +09:00

53 lines
1.7 KiB
TypeScript

import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
/**
* Next.js 미들웨어
* 페이지 렌더링 전에 실행되어 인증 상태를 확인하고 리다이렉트 처리
*
* 주의: 미들웨어는 쿠키만 접근 가능 (localStorage 접근 불가)
* 따라서 토큰이 localStorage에만 있고 쿠키에 없는 경우는
* 클라이언트 사이드의 AuthGuard에서 처리
*/
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const token = request.cookies.get("authToken")?.value;
// /login 페이지 접근 시 - 토큰이 있으면 메인으로
if (pathname === "/login") {
if (token) {
const url = request.nextUrl.clone();
url.pathname = "/main";
return NextResponse.redirect(url);
}
return NextResponse.next();
}
// 보호 경로 목록 - 쿠키에 토큰이 없으면 로그인으로
// 단, 쿠키 없이 localStorage에만 토큰이 있을 수 있으므로
// 클라이언트에서 한 번 더 확인 후 리다이렉트 (AuthGuard)
const strictProtectedPaths = ["/admin"];
const isStrictProtected = strictProtectedPaths.some((path) => pathname.startsWith(path));
if (isStrictProtected && !token) {
const url = request.nextUrl.clone();
url.pathname = "/login";
return NextResponse.redirect(url);
}
// /main, /screens, /dashboard 등은 쿠키 없어도 통과 허용
// (localStorage 토큰이 있을 수 있으므로 클라이언트 AuthGuard에 위임)
return NextResponse.next();
}
/**
* 미들웨어가 실행될 경로 설정
*/
export const config = {
matcher: [
"/((?!api|_next/static|_next/image|favicon.ico|.*\\.png$|.*\\.jpg$|.*\\.jpeg$|.*\\.svg$).*)",
],
};