Session is null in Nextjs 15 middleware (Express backend) only in prod

Hi guys, I am able to get session in middleware with nodejs runtime in my dev server, but when I deploy in production it doesn't seem to pass any cookies when I make the request to the expressjs auth server.. What is the issue? Here's my

middleware.ts

import { NextRequest, NextResponse } from "next/server"
import { headers } from "next/headers"
import { authClient } from "@/lib/auth"

export async function middleware(request: NextRequest) {
  const { pathname } = request.nextUrl

  const nextHeaders = await headers()

  const session = await authClient.getSession({
    fetchOptions: {
      headers: nextHeaders,
    },
  })

  console.log({ session })

  if (pathname.startsWith("/dashboard") && !session.data) {
    return NextResponse.redirect(new URL("/login", request.url))
  }
  return NextResponse.next()
}

export const config = {
  runtime: "nodejs",
  matcher: ["/dashboard/:path*"],
}
Solution
So I managed to solve this issue. Problem was I was using cross subdomains (different subdomain in frontend and backend). Emphasis on crossSubDomainCookies and defaultCookieAttributes
Was this page helpful?