Fix middleware cookie detection behind HTTPS reverse proxy
getToken() defaults secureCookie to false, so it looks for "authjs.session-token" cookie. Behind Traefik over HTTPS, Auth.js sets "__Secure-authjs.session-token". Detect HTTPS via x-forwarded-proto header and pass secureCookie accordingly. Made-with: Cursor
This commit is contained in:
parent
04e9811572
commit
48959190a0
1 changed files with 8 additions and 1 deletions
|
|
@ -44,7 +44,14 @@ export async function middleware(req: NextRequest) {
|
||||||
return NextResponse.next();
|
return NextResponse.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
const token = await getToken({ req, secret: process.env.AUTH_SECRET });
|
const secureCookie =
|
||||||
|
req.headers.get("x-forwarded-proto") === "https" ||
|
||||||
|
req.nextUrl.protocol === "https:";
|
||||||
|
const token = await getToken({
|
||||||
|
req,
|
||||||
|
secret: process.env.AUTH_SECRET,
|
||||||
|
secureCookie,
|
||||||
|
});
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
const loginUrl = new URL("/login", req.url);
|
const loginUrl = new URL("/login", req.url);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue