import { NextRequest, NextResponse } from "next/server"; import { CS_URL } from "./components/lib/config"; //import { CLAIM_NAME, AUTH_USER_ID } from "./src/shared/config"; const CLAIM_NAME = "Authorization" const AUTH_USER_ID = "token-id" const serverApiUrl = CS_URL; export function middleware(req: NextRequest) { const pathname = req.nextUrl.pathname; console.log(`[Middleware] Request URL: ${req.url}`); const searchParams = req.nextUrl.searchParams; if (pathname.startsWith("/server")) { const queryParams = searchParams.toString(); const newUrl = `${serverApiUrl}${pathname.replace("/server", "")}${queryParams ? `?${queryParams}` : "" }`; console.log(`[Middleware] Rewriting from '/server' path to: ${newUrl}`); return NextResponse.rewrite(new URL(newUrl)); } if (pathname.startsWith("/attach-service")) { const requestHeaders = new Headers(req.headers); const token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhMGVjN2EwNy03NTJhLTRmN2YtODlhNi04NTRlNjNkMmUzYTAiLCJhdXRob3JpdGllcyI6IlJPTEVfQURNSU4sUk9MRV9BTk9OWU1PVVMiLCJleHAiOjE3NDg0Mzc0OTF9.JM2NHEwTpgDW9nonZY3186IK_frIh667SyxMQfPwqa_lc-5mcTFNtrWXH5b72_ng39qiPSi504iF1roFEJ0c6w" const userId = 'a0ec7a07-752a-4f7f-89a6-854e63d2e3a0' //const token = req.cookies.get(CLAIM_NAME)?.value; //const userId = req.cookies.get(AUTH_USER_ID)?.value; const newUrl = `${serverApiUrl}${pathname}`; console.log("serverApiUrl ", serverApiUrl); console.log("pathname ", pathname); //wr:dev: pathname /attach-service/api/v1/tus/upload/5e60b70a-956e-40c3-be5a-cdd3100bf8ad //http://dev.onasys.co.kr:8000/attach-service/api/v1/tus/upload/5e60b70a-956e-40c3-be5a-cdd3100bf8ad if (token) requestHeaders.set(CLAIM_NAME, token); if (userId) requestHeaders.set(AUTH_USER_ID, userId); console.log( `[Middleware] Rewriting from '/attach-service' path to: ${newUrl}`, ); return NextResponse.rewrite(new URL(newUrl), { headers: requestHeaders, }); } return NextResponse.next(); } export const config = { matcher: ["/server/:path*", "/attach-service/:path*"], };