How to get a session in NextJS Middleware

Hi @relero90

Thank you for reaching out to us!

Could you try changing your middleware function like this and let me know if the results change?

export async function middleware(request: NextRequest) {
  const { origin } = new URL(request.url)
  const res = await auth0.middleware(request)
  const session = await auth0.getSession()

  if (!session && !request.nextUrl.pathname.startsWith("/auth")) {
    console.log("No session found, redirecting to login")
    return NextResponse.redirect(`${origin}/auth/login`)
  }

  return res
}

I would suggest looking through the following Post as it was on a similar use-case.

Hope this helped!
Gerald