nextjs-auth0: Documentation to use refresh tokens is missing some information
Checklist
- I have looked into the README and have not found a suitable solution or answer.
- I have looked into the examples and have not found a suitable solution or answer.
- I have looked into the API documentation and have not found a suitable solution or answer.
- I have searched the issues and have not found a suitable solution or answer.
- I have searched the Auth0 Community forums and have not found a suitable solution or answer.
- I agree to the terms within the Auth0 Code of Conduct.
Describe the problem you’d like to have solved
I tried setting up refresh tokens following the instructions (adding offline_access to my AUTH0_SCOPE environment variable), and I thought it was working. However once the token expired, I would see issues when trying to use the getAccessToken method on my API route.
It wasn’t exactly clear that I hadn’t setup refresh tokens correctly because the token was being issues with all of the other scopes.
In the end I had noticed that on Auth0 I hadn’t enabled refresh tokens for my API, but I did enable them for my application.
Describe the ideal solution
- Better documentation, explaining that one must:
- include
offline_accessscope in their scope list (either environment variableAUTH0_SCOPEor provided manually) - enable refresh token grant type for their application:

- allow offline access for their API(s) associated with the application

- include
- Throw error when granted token doesn’t include all the requested scopes so that one can find the issue
Alternatives and current workarounds
No response
Additional context
No response
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 1
- Comments: 20 (5 by maintainers)
Also - one issue I noticed is that the
withPageAuthRequiredwasn’t redirecting a user to login when they had an expired session/JWT, but would fail on thegetAccessTokenmethod.One would expect that
withPageAuthRequiredwould redirect users when they have an expired session@ci-vamp asking as well, if you dealt with the problem.
Our situation is pretty much 1:1 with what you’ve been describing. We moved from Gatsby SPA front end silent auth that was working fine getting tokens for our external API, now to Next JS. I’ve tried using regular server side functions, then API routes, and middleware, as I thought read-only cookies on SSR were the issue but to no avail, all have the same refresh token issue.
I think it doesn’t fail on the first refresh but rather on the subsequent. The token gets invalidated but the session persists so user is no longer receiving data even though it appears he’s logged in and everything is fine.
Tried tons of options and variations of settings, keep updating to latest libs, but nothing really gets me there. Trying to debug this from time to time the past 2-3months now as the release deadline approaches.
Ultimately my guess is it could be that the next js lib is sending multiple refresh token requests at same time and it does work on the first request, but then immediately after it wants to refresh token while using token that was just used and it blocks out any further requests. I don’t really see how I could prevent that from happening though.
Would appreciate any inputs
@ci-vamp hello, did you find a solution/workaround for this problem? We are dealing with the same.
Yes that was for debugging. I have tried every combination I could think of. Increasing the expiration (out to the max of 365.5 days) delays it but eventually it occurs again.
I don’t have withApiAuthRequired because I am protecting globally with the middleware variant. I also tried moving the call to the middleware to see if that was related but it occurs there too. In fact the reason I moved to trying the middleware was because originally I wrapped each page and route individually and I thought using the middleware would fix it. Or at least I’d be able to redirect on the error (but this also doesn’t work - I run into CORS issues trying to redirect from a api proxy call to the login page).
What I have tried:
refresh: truetogetAccessTokenscope(as an array) instead of in the auth params (as space separated list)Every time I changed a0 settings I waited 1 min and did a fresh logout + log in.
What I have noticed:
I have read every a0 community and GH issue related to this subject - everything is purple. I have tried every recommended setting and approach. I’m really at a loss.
The only thing I can think of is that the api proxy is trying to create an access token for every request that comes in. This leads to some overload of the token endpoint for data heavy pages (our heaviest makes 35 calls on load).
Ideally I would use the access token on the session then refresh as it approaches expiration (“silent auth” as it was done in a0 spa lib) but I can’t figure out how to do this without redirecting to the login page periodically (see observations above).
We recently migrated from a SPA to nextjs and are preparing for a prod rollout in a few weeks. This bug is now putting it on hold. Because of its inconsistent nature I can’t give the green light (or go into prod with absolute lifetime disabled).
@adamjmcgrath Any way you can help me on this? I’ve done the most diligence I can and spent the past 4 days grinding on it.
@joelzwarrington i am running into this issue as well.
We use withMiddlewareAuthRequired to protect all routes (pages and API). The middleware only checks if there is a valid session but not if the access token is valid.
We have an API proxy route which proxies calls to our backend API and attached an access token (since these aren’t accessible client side). When an API route is reached (passing middleware check because the session exists) it uses
getAccessToken. This works for some time but eventually it starts throwing this errorI tried using
getSessionin the API route and noticed it does have an access token but it is invalid.I also noticed that if I manually navigate to /api/auth/login it fixes the issue without requiring the user to authenticate again (it goes to the universal login page then immediately redirects back to the app).
For our use case a session is effectively 1:1 with an access token - the app only works by making calls to the backend.
I have 2 questions