AccessTokenError: The user does not have a valid access token

Hi all

I’m trying to call my external api in my server side rendered page, with an access_token because access requires a certain scope.

const tokenCache = auth0.tokenCache(context.req, context.res);
const { accessToken } = await tokenCache.getAccessToken({
  scopes: ['read:messages']
});

todos = await axios.get("http://localhost:3010/api/private", {
  'Authorization' : `Bearer ${accessToken}`
})
  .then(response => todos = response.data)
  .catch(error => console.error(error));

I added audience in my initAuth file like here https://github.com/auth0/nextjs-auth0/issues/90
The value of my audience is the same as it is in the API settings.

But I get the error in the title of this post…

Hi @ibi

The access token could be invalid for many reasons. Start by verifying the access token with jwt.io, then take a look at the API code that validates the access token to see if you can determine what is failing.

John

const { accessToken } = await tokenCache.getAccessToken({
scopes: [‘read:messages’]
});

This line is failing. Before I can log a console statement, but afterwards it’s not possible because this throws the error. Do you know why maybe?

What are in context.req and context.res?

John

Hi John,

Thanks for the reply. I took another approach and downloaded a nextjs boilerplate.
Things work there with SSR, don’t know why it doesn’t here, so I don’t need the answer here anymore. Thanks anyways.

1 Like

Thanks for letting us know!

hi @ibi, I’m having the same issue. Which boilerplate did you use? And any chance you found the exact issue? I"m halfway through my project, moving to a new boilerplate will be a pain in the ass. Thanks in advance