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)); }