Skip to content

Can't get JWT for user #1984

Closed
Closed
@Azer0s

Description

@Azer0s

Checklist

Description

I am currently trying to get the JWT for a user to later use against a Java Spring backend but unfortunately, nothing has worked so far.

export default async function RootLayout({
  children,
}: Readonly<{ children: React.ReactNode }>) {
  let session: SessionData | null = null;
  try {
    session = await auth0.getSession();

    const { token } = await getAccessToken();
    console.log(token);
  } catch (err) {
    void err;
  }
...

This always causes the following error:

[TypeError: Failed to parse URL from /auth/access-token] {
  [cause]: [TypeError: Invalid URL] {
    code: 'ERR_INVALID_URL',
    input: '/auth/access-token'
  }
}

When I tried manually getting the token via fetch like so

export default async function RootLayout({
  children,
}: Readonly<{ children: React.ReactNode }>) {
  let session: SessionData | null = null;
  try {
    session = await auth0.getSession();

    const host = (await headers()).get("host");

    const tokenRes = await fetch(`http://${host}/auth/access-token`).then(
      (res) => res.json()
    );
    console.log(tokenRes);
  } catch (err) {
    void err;
  }
...

I get another error message telling me that the user does not have an active session.

{
  error: {
    message: 'The user does not have an active session.',
    code: 'missing_session'
  }
}

Reproduction

As seen above

Additional context

I can get the session with await auth0.getSession() but when I try to access the token, I get a JWE instead of a JWT. The AUTH0_AUDIENCE is set in my .env.local file.

nextjs-auth0 version

^4.0.3

Next.js version

15.2.1

Node.js version

23.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions