# Auth0 getAccessToken() and getSession() Not Working in Next.js 15 App Router (Server & Client Components)

**URL:** <https://community.auth0.com/t/auth0-getaccesstoken-and-getsession-not-working-in-next-js-15-app-router-server-client-components/185745>\
**Category:** Get Help\
**Tags:** sessions, access-token\
**Created:** [April 21, 2025, 11:30am UTC](https://community.auth0.com/t/auth0-getaccesstoken-and-getsession-not-working-in-next-js-15-app-router-server-client-components/185745 "2025-04-21T11:30:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nithinsriharir](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nithinsriharir/32/73595_2.png) [@nithinsriharir](https://community.auth0.com/u/nithinsriharir)\
**Post date:** [April 21, 2025, 11:30am UTC](https://community.auth0.com/t/auth0-getaccesstoken-and-getsession-not-working-in-next-js-15-app-router-server-client-components/185745/1 "2025-04-21T11:30:29Z")

</div>

I’m having persistent issues integrating `@auth0/nextjs-auth0` (v4.4.2) with a Next.js 15 project (using the App Router). The main problem:

- Calling `getAccessToken()` or `getSession()` in API routes, server actions, or server components throws:

TypeError: Failed to parse URL from /auth/access-token

- The same error occurs when attempting to use these methods in client components (via fetch to an API route).
- Auth0 setup (provider, env vars, etc.) appears correct and authentication redirects work, but retrieving the access token/session on the server side is broken.
- Wrapping components in `<Suspense>` or using the recommended `UserProvider` does not help.
- I am using Turbopack (`next dev --turbopack`), but the problem persists even without it.
- Installing the suggested peer dependency `@edge-runtime/cookies` does not resolve the issue.

**What I’ve tried:**

- Creating a dedicated API route to call and log `getAccessToken()`.
- Using both server and client components to fetch the access token/session.
- Ensuring correct Auth0 configuration and session setup.

**Versions:**

- `next`: 15.3.0
- `@auth0/nextjs-auth0`: 4.4.2

Request:  
Is there a known compatibility issue between Auth0’s SDK and Next.js 15 App Router? Are there any workarounds or recommended approaches to retrieve the access token/session in this setup? Any help or guidance would be appreciated!

* * *

Let me know if you want to add more details or clarify anything further!

NOTE: All other features (Login/Signup/Signout and profile ) work fine just the route:  
/auth/access-token does not in Nextjs App router.

---

<div class="post-metadata">

**Author:** ![vlad.murarasu](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vlad.murarasu/32/72793_2.png) [@vlad.murarasu](https://community.auth0.com/u/vlad.murarasu)\
**Post date:** [April 22, 2025, 1:59pm UTC](https://community.auth0.com/t/auth0-getaccesstoken-and-getsession-not-working-in-next-js-15-app-router-server-client-components/185745/2 "2025-04-22T13:59:04Z")

</div>

Hi @nithinsriharir,

Welcome to the Auth0 Community!

Make sure you’ve set up the SDK correctly by checking our quickstart guide:

> **[Auth0 Next.js SDK Quickstarts: Add Login to Your Next.js Application](https://auth0.com/docs/quickstart/webapp/nextjs/interactive)**
>
> This guide demonstrates how to integrate Auth0 with any new or existing Next.js application using the Auth0 Next.js v4 SDK (Beta).

Let us know if this fixes your issue.

Also could you provide the snippet of code where the error occurs?

Have a good one,  
Vlad

---

<div class="post-metadata">

**Author:** ![nithinsriharir](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nithinsriharir/32/73595_2.png) [@nithinsriharir](https://community.auth0.com/u/nithinsriharir)\
**Post date:** [April 22, 2025, 2:45pm UTC](https://community.auth0.com/t/auth0-getaccesstoken-and-getsession-not-working-in-next-js-15-app-router-server-client-components/185745/4 "2025-04-22T14:45:25Z")

</div>

Yes, I’ve followed the guide. All other pre-defined endpoints work fine except /auth/access-token  
Here are the .env vars saved:

```auto
AUTH0_SECRET={SECRET}
APP_BASE_URL=http://localhost:3000
AUTH0_DOMAIN=dev-rmhdvcklahazorlu.us.auth0.com
AUTH0_CLIENT_ID={CLIENT_ID}
AUTH0_CLIENT_SECRET={CLIENT_SECRET}
AUTH0_AUDIENCE=https://api.qhsse.com
AUTH0_SCOPE=openid profile email

```

Here is my code snippet from lib/auth0-token.ts

```auto
import { auth0 } from "./auth0";

export async function fetchAccesstoken() {
  try {
    const session = await auth0.getSession();
    if (!session) {
      throw new Error("Not authenticated");
    }
    const { token: accessToken } = await auth0.getAccessToken();
    return { accessToken };
  } catch (error: any) {
    return { error: error.message, status: error.status || 500 };
  }
}

```

Calling this ` const accessToken = await fetchAccesstoken(); console.log('sample',accessToken)`

Always returns a JWE token, It seems, I’m not allowed to pass the AUTH0\_AUDIENCE param when calling getAccessToken in ver.4.x + of next.auth0

I’ve also tried the same by cloning the provided example project:

> **[GitHub - auth0-samples/auth0-nextjs-samples: Auth0 Integration Samples for...](https://github.com/auth0-samples/auth0-nextjs-samples/tree/main)**
>
> main

and applying my .env variables but this also still results the same issue.

Am I missing something here? perhaps in the auth0.dashboard?  
I’ve also authorised the app from the API settings (machine to machine applications) and also ensured that the option to pass JWE Encrypting the signed access\_token issued for this API is ‘Disabled’

But its still passing a JWE token instead of a JWT. (Which is not possible for me to handle in the Backend)

---

<div class="post-metadata">

**Author:** ![vlad.murarasu](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vlad.murarasu/32/72793_2.png) [@vlad.murarasu](https://community.auth0.com/u/vlad.murarasu)\
**Post date:** [April 22, 2025, 5:00pm UTC](https://community.auth0.com/t/auth0-getaccesstoken-and-getsession-not-working-in-next-js-15-app-router-server-client-components/185745/5 "2025-04-22T17:00:45Z")

</div>

Hi @nithinsriharir,

Thank you for this info.

Looking further into it I found a great post that seems to address this issue:

> [@The getAccessToken in Nextjs returns opaque token instead of access token even after specifying the audience](https://community.auth0.com/t/the-getaccesstoken-in-nextjs-returns-opaque-token-instead-of-access-token-even-after-specifying-the-audience/146457/14):
>
> Hi @vishnukumart96 Welcome to the Auth0 Community and I am sorry about the late reply to your inquiry! For anyone who may be looking for a solution to this issue, I would recommend to review this community post regarding receiving an opaque access token: [@auth0/nextjs-auth0 tokens and external api help](https://community.auth0.com/t/auth0-nextjs-auth0-tokens-and-external-api-help/71072) In general, an opaque access token is returned because the right audience or scope was not passed in as parameters, the application was not registered with the Management API or the right gra…

Let us know if this solves your issue.

Have a good one,  
Vlad

---

<div class="post-metadata">

**Author:** ![nithinsriharir](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nithinsriharir/32/73595_2.png) [@nithinsriharir](https://community.auth0.com/u/nithinsriharir)\
**Post date:** [April 22, 2025, 6:06pm UTC](https://community.auth0.com/t/auth0-getaccesstoken-and-getsession-not-working-in-next-js-15-app-router-server-client-components/185745/6 "2025-04-22T18:06:43Z")

</div>

Hi @vlad.murarasu ,

I looked into the same forum post earlier.  
The newer version of “@auth0/nextjs-auth0”: “4.2.2” supports app router for Next.js.  
The solution you referenced fixes the issue where an opaque access token is returned because the right `audience` or `scope` was not passed in as parameters (which works for pages router). However, for Next.js app router (or at least the newer version 4.x+), it does not allow passing the audience or scope env directly to auth0.getAccessToken().

Which likely causes the issue. I’ve also noted that in the latest versions of  
@auth0/nextjs-auth0 (v4+), the SDK expects the audience and scope to be set globally during Auth0 client initialization, not per-request as in earlier versions.

i.e as per the example shown in the [GitHub - auth0-samples/auth0-nextjs-samples: Auth0 Integration Samples for Next.js Applications](https://github.com/auth0-samples/auth0-nextjs-samples/tree/main)

```auto
import { Auth0Client } from "@auth0/nextjs-auth0/server";

// Initialize the Auth0 client 
export const auth0 = new Auth0Client({
  // Options are loaded from environment variables by default
  // Ensure necessary environment variables are properly set
  // domain: process.env.AUTH0_DOMAIN,
  // clientId: process.env.AUTH0_CLIENT_ID,
  // clientSecret: process.env.AUTH0_CLIENT_SECRET,
  // appBaseUrl: process.env.APP_BASE_URL,
  // secret: process.env.AUTH0_SECRET,
  authorizationParameters: {
    // In v4, the AUTH0_SCOPE and AUTH0_AUDIENCE environment variables are no longer automatically picked up by the SDK.
    // Instead, we need to provide the values explicitly.
    scope: process.env.AUTH0_SCOPE,
    audience: process.env.AUTH0_AUDIENCE,
  }
});

```

Not sure how to proceed from here..  
Please let me know if you want me to add more details or clarify anything further!

---

<div class="post-metadata">

**Author:** ![ben.viranin](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ben.viranin/32/72153_2.png) [@ben.viranin](https://community.auth0.com/u/ben.viranin)\
**Post date:** [April 29, 2025, 4:11pm UTC](https://community.auth0.com/t/auth0-getaccesstoken-and-getsession-not-working-in-next-js-15-app-router-server-client-components/185745/7 "2025-04-29T16:11:51Z")

</div>

Hello,

Has any advancements been made on this issue ?

---

<div class="post-metadata">

**Author:** ![carlosbuydepa](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/carlosbuydepa/32/72928_2.png) [@carlosbuydepa](https://community.auth0.com/u/carlosbuydepa)\
**Post date:** [June 4, 2025, 8:29pm UTC](https://community.auth0.com/t/auth0-getaccesstoken-and-getsession-not-working-in-next-js-15-app-router-server-client-components/185745/8 "2025-06-04T20:29:52Z")

</div>

Hello,

I faced the same problem trying to get the access token from the app router.

- As of now, @auth0/nextjs-auth0 does not support getAccessToken() in App Router route handlers because it requires Node.js req/res.

- You would need to keep your token API route in the Pages Router, or

Do this:

app/api/auth/token/route.ts

```auto
import { auth0 } from "@/lib/auth0";
import { NextResponse, NextRequest } from "next/server";

export async function GET(req: NextRequest) {
  const session = await auth0.getSession();
  return NextResponse.json({ accessToken: session?.tokenSet.accessToken });
}

```

Notice that the accessToken is a property, not a function.

---

<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:** [June 19, 2025, 4:45pm UTC](https://community.auth0.com/t/auth0-getaccesstoken-and-getsession-not-working-in-next-js-15-app-router-server-client-components/185745/9 "2025-06-19T16:45:00Z")

</div>

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