Fix localhost redirect loops in proxy middleware using nextUrl.clone() and trustHost
This commit is contained in:
1
auth.ts
1
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: {
|
||||
|
||||
8
proxy.ts
8
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();
|
||||
|
||||
Reference in New Issue
Block a user