From 991593c301cdc09d03665cd445153070594cf89b Mon Sep 17 00:00:00 2001 From: NotII <46204250+NotII@users.noreply.github.com> Date: Sun, 23 Mar 2025 22:20:43 +0000 Subject: [PATCH] Update middleware.ts --- middleware.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/middleware.ts b/middleware.ts index 30baafd..6b07ae1 100644 --- a/middleware.ts +++ b/middleware.ts @@ -12,12 +12,17 @@ export async function middleware(req: NextRequest) { console.log("Middleware: Token found, validating..."); try { - // Use our internal API route that handles the auth check server-side - // This avoids SSL issues as it's a same-origin request + // Get the origin but handle localhost differently to avoid SSL issues const origin = req.nextUrl.origin; - const authCheckUrl = `${origin}/api/auth/check`; - console.log(`Middleware: Using internal auth check URL: ${authCheckUrl}`); + // Construct the auth check URL based on environment + // For localhost, explicitly use HTTP instead of HTTPS + const isLocalhost = origin.includes('localhost') || origin.includes('127.0.0.1'); + const protocol = isLocalhost ? 'http' : 'https'; + const host = req.nextUrl.host; + const authCheckUrl = `${protocol}://${host}/api/auth/check`; + + console.log(`Using internal auth check URL: ${authCheckUrl}`); const res = await fetch(authCheckUrl, { method: "GET", @@ -38,7 +43,7 @@ export async function middleware(req: NextRequest) { console.log("Middleware: Auth check successful, proceeding to dashboard"); } catch (error) { - console.error("Middleware: Authentication validation failed:", error); + console.error("Authentication validation failed:", error); return NextResponse.redirect(new URL("/auth/login", req.url)); }