# How to get a session in NextJS Middleware

**URL:** <https://community.auth0.com/t/how-to-get-a-session-in-nextjs-middleware/188482>\
**Category:** Get Help\
**Tags:** sdks-quickstarts, nextjs-auth0\
**Created:** [June 26, 2025, 11:19pm UTC](https://community.auth0.com/t/how-to-get-a-session-in-nextjs-middleware/188482 "2025-06-26T23:19:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![relero90](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/relero90/32/72930_2.png) [@relero90](https://community.auth0.com/u/relero90)\
**Post date:** [June 26, 2025, 11:19pm UTC](https://community.auth0.com/t/how-to-get-a-session-in-nextjs-middleware/188482/1 "2025-06-26T23:19:24Z")

</div>

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…

```auto
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?

---

<div class="post-metadata">

**Author:** ![gerald.czifra](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/gerald.czifra/32/75301_2.png) [@gerald.czifra](https://community.auth0.com/u/gerald.czifra)\
**Post date:** [June 30, 2025, 6:26pm UTC](https://community.auth0.com/t/how-to-get-a-session-in-nextjs-middleware/188482/3 "2025-06-30T18:26:37Z")

</div>

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?

```auto
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](https://community.auth0.com/t/error-error-cookies-was-called-outside-a-request-scope/188318/3) as it was on a similar use-case.

Hope this helped!  
Gerald

---

<div class="post-metadata">

**Author:** ![relero90](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/relero90/32/72930_2.png) [@relero90](https://community.auth0.com/u/relero90)\
**Post date:** [June 30, 2025, 9:35pm UTC](https://community.auth0.com/t/how-to-get-a-session-in-nextjs-middleware/188482/4 "2025-06-30T21:35:21Z")

</div>

Thank you. The post you linked provided the correct clue. It is now necessary to pass the request object to `getSession`:

```auto
export async function middleware(request: NextRequest) {
   ...

    const session = await auth0.getSession(request);

    ...
}

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [July 14, 2025, 9:35pm UTC](https://community.auth0.com/t/how-to-get-a-session-in-nextjs-middleware/188482/5 "2025-07-14T21:35:47Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
