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:
Randall Stillwell 2026-04-15 14:11:50 -05:00
parent 04e9811572
commit 48959190a0

View file

@ -44,7 +44,14 @@ export async function middleware(req: NextRequest) {
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) {
const loginUrl = new URL("/login", req.url);