Requesting accessToken not returning a valid JWT

I have a NextJS app that uses the @auth0/nextjs-auth0 as its authentication library. I’m trying to get an access token but the returned JWT is not a valid object according to jwt.io.

For reference, this is my .env.local file:

AUTH0_SECRET='secret'
AUTH0_BASE_URL='http://localhost:3000'
AUTH0_ISSUER_BASE_URL='domain'
AUTH0_CLIENT_ID='client'
AUTH0_CLIENT_SECRET='clientsecret'
AUTH0_SCOPE='openid profile email'

And here is a super basic API route I have:

import { NextApiRequest, NextApiResponse } from "next";
import { getAccessToken } from "@auth0/nextjs-auth0";

export default async (req: NextApiRequest, res: NextApiResponse) => {
  const token = await getAccessToken(req, res, {
    scopes: ["openid", "profile", "email"],
  });

  res.status(200).json(token);
};

What am I doing wrong here?

Hello @halsdunes,

I looks like you are not including an audience string in your request. Without an audience string Auth0 will return an opaque access token which can be used only for querying the userinfo endpoint.

Hi @markd,

I updated my .env.local like so:

AUTH0_SECRET='secret'
AUTH0_BASE_URL='http://localhost:3000'
AUTH0_ISSUER_BASE_URL='domain'
AUTH0_CLIENT_ID='client'
AUTH0_CLIENT_SECRET='clientsecret'
AUTH0_AUDIENCE='test'
AUTH0_SCOPE='openid profile email'

but now, when I attempt to log in, I get an error saying

access_denied (Service not found: test)

what should the audience value correspond to?

1 Like

I was able to figure it out using this previous answer!

For anyone else that might be reading this, I went to the API section of the Auth0 dashboard and copied the identifier value as the AUTH0_AUDIENCE value in my .env.local file. You will need to restart the Next.js server and re-authenticate for the changes to work.

Thanks for setting me in the right direction @markd!

2 Likes

Wooohoo perfect! Teamwork makes the dreamwork!

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