I have an application that was set up using NextJS 15.3.1 and the 4.0.0-beta.0 nextjs-auth0 SDK. I can see that this SDK has been brought into a stable version and am attempting to update to use it but am running into an issue.
How can I access a session object from my middleware? My original middleware setup was something like…
export async function middleware(request: NextRequest) {
const businessUnit = detectBusinessUnitFromRequest(request);
const auth0 = await createAuth0Client(businessUnit);
const authResponse = await auth0.middleware(request);
if (request.nextUrl.pathname.startsWith('/auth')) {
return authResponse;
}
const session = await auth0.getSession();
// assorted route access checks based on user permissions from the session object
return authResponse;
}
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)"],
}
That await auth0.getSession()
now throws a NextJS error for calling cookies
outside of a request scope. What is the alternative? How else can I access the users data and access tokens here in the middleware?