Fix redirect host resolution in proxy.ts behind reverse proxy
This commit is contained in:
13
proxy.ts
13
proxy.ts
@@ -6,16 +6,17 @@ export default auth((req) => {
|
||||
const isLoggedIn = !!req.auth;
|
||||
const isLoginPage = nextUrl.pathname === "/login";
|
||||
|
||||
// Proxy arkasında çalışırken doğru adresi alabilmek için
|
||||
const host = req.headers.get("x-forwarded-host") || req.headers.get("host") || "localhost:3000";
|
||||
const proto = req.headers.get("x-forwarded-proto") || "http";
|
||||
const baseUrl = `${proto}://${host}`;
|
||||
|
||||
if (!isLoggedIn && !isLoginPage) {
|
||||
const url = nextUrl.clone();
|
||||
url.pathname = "/login";
|
||||
return NextResponse.redirect(url);
|
||||
return NextResponse.redirect(new URL("/login", baseUrl));
|
||||
}
|
||||
|
||||
if (isLoggedIn && isLoginPage) {
|
||||
const url = nextUrl.clone();
|
||||
url.pathname = "/dashboard";
|
||||
return NextResponse.redirect(url);
|
||||
return NextResponse.redirect(new URL("/dashboard", baseUrl));
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
|
||||
Reference in New Issue
Block a user