From ef2f1efcd0e114d009acb90aba0e7a4e0110505d Mon Sep 17 00:00:00 2001 From: Bagasbanuna02 Date: Tue, 8 Apr 2025 11:40:16 +0800 Subject: [PATCH] fix middleware 3 --- src/middleware.ts | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/middleware.ts b/src/middleware.ts index 96dfa162..8bbd52cc 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -129,8 +129,11 @@ export const middleware = async (req: NextRequest) => { } try { + // const origin = new URL(req.url).origin; + // console.log("origin", origin); + const validationResponse = await fetch( - new URL(validationApiRoute, req.url), + `${new URL(req.url).origin}/api/validation`, { headers: { "Content-Type": "application/json", @@ -151,12 +154,15 @@ export const middleware = async (req: NextRequest) => { // Handle /dev routes that require active status if (pathname.startsWith("/dev")) { try { - const userValidate = await fetch(new URL("/api/user-validate", req.url), { - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${token}`, - }, - }); + const userValidate = await fetch( + new URL(req.url).origin + "/api/user-validate", + { + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + } + ); if (!userValidate.ok) { throw new Error("Failed to validate user"); @@ -227,10 +233,13 @@ function unauthorizedResponse() { } function unauthorizedResponseTokenAPI() { - return new NextResponse(JSON.stringify({ error: "Unauthorized token on API" }), { - status: 401, - headers: { "Content-Type": "application/json" }, - }); + return new NextResponse( + JSON.stringify({ error: "Unauthorized token on API" }), + { + status: 401, + headers: { "Content-Type": "application/json" }, + } + ); } function unauthorizedResponseTokenPAGE() {