From 114bbbba81ad4b9fe6ef297319af6075a24fe7b9 Mon Sep 17 00:00:00 2001 From: AyrisAI Date: Thu, 14 May 2026 02:15:00 +0300 Subject: [PATCH] Fix localhost redirect loops in proxy middleware using nextUrl.clone() and trustHost --- auth.ts | 1 + proxy.ts | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/auth.ts b/auth.ts index e88dba6..1d7e409 100644 --- a/auth.ts +++ b/auth.ts @@ -3,6 +3,7 @@ import Credentials from "next-auth/providers/credentials"; import { authenticateUser } from "@/lib/users"; export const { handlers, signIn, signOut, auth } = NextAuth({ + trustHost: true, providers: [ Credentials({ credentials: { diff --git a/proxy.ts b/proxy.ts index a2390da..cc4c2f7 100644 --- a/proxy.ts +++ b/proxy.ts @@ -7,11 +7,15 @@ export default auth((req) => { const isLoginPage = nextUrl.pathname === "/login"; if (!isLoggedIn && !isLoginPage) { - return NextResponse.redirect(new URL("/login", req.url)); + const url = nextUrl.clone(); + url.pathname = "/login"; + return NextResponse.redirect(url); } if (isLoggedIn && isLoginPage) { - return NextResponse.redirect(new URL("/dashboard", req.url)); + const url = nextUrl.clone(); + url.pathname = "/dashboard"; + return NextResponse.redirect(url); } return NextResponse.next();