Fix login redirect missing basePath behind reverse proxy
Use request.nextUrl.clone() instead of new URL("/login", request.url)
so Next.js includes the /hp-prod-tracker basePath in redirects.
Without this, unauthenticated users get sent to /login instead of
/hp-prod-tracker/login.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
58d8459b43
commit
30f804b7ff
1 changed files with 6 additions and 2 deletions
|
|
@ -52,7 +52,9 @@ export function middleware(request: NextRequest) {
|
|||
|
||||
// Redirect logged-in users away from login page
|
||||
if (isAuthPage && isLoggedIn) {
|
||||
return NextResponse.redirect(new URL("/dashboard", request.url));
|
||||
const url = request.nextUrl.clone();
|
||||
url.pathname = "/dashboard";
|
||||
return NextResponse.redirect(url);
|
||||
}
|
||||
|
||||
// Allow authenticated users to access the pending page
|
||||
|
|
@ -65,7 +67,9 @@ export function middleware(request: NextRequest) {
|
|||
if (pathname.startsWith("/api/")) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
return NextResponse.redirect(new URL("/login", request.url));
|
||||
const url = request.nextUrl.clone();
|
||||
url.pathname = "/login";
|
||||
return NextResponse.redirect(url);
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue