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

  1. Better documentation, explaining that one must:
    1. include offline_access scope in their scope list (either environment variable AUTH0_SCOPE or provided manually)
    2. enable refresh token grant type for their application: image
    3. allow offline access for their API(s) associated with the application image
  2. 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)

Most upvoted comments

Also - one issue I noticed is that the withPageAuthRequired wasn’t redirecting a user to login when they had an expired session/JWT, but would fail on the getAccessToken method.

One would expect that withPageAuthRequired would 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:

  • RTR from 0 to 90 seconds
  • RTR disabled
  • absolute expiration from 30s to 365.5 days - the longer the period the longer it works before hitting the error again. At 30s I can hit it consistently
  • disabling absolute expiration - this is not acceptable for our service but it did fix the issue
  • getting access token in the api proxy
  • getting access token in the middleware and sending it over through an x-header (had to upgrade to 3.1 to try this)
  • adding refresh: true to getAccessToken
  • using the top level scope (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:

  • when the call fails with the error described above, the a0 logs show the ftrr error with no connection listed.
  • extending / disabling absolute expiration seems to work. Despite having RTR disabled i saw constant (dozens) of refresh token success logs.
  • when they succeed it has the connection name and ID. I don’t know what this means
  • if I forcibly log in it (redirecting to /api/auth/login from middleware) fixes it. But this only works on page routes. For api routes I get a CORS error from a0 so I can’t use this solution.

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.

hey @adamjmcgrath, thanks for reaching out, I’ll look to setup an example which reproduces my issue.

But if you want to try and reproduce:

  • refresh token grant enabled on application

  • offline access disabled on api

  • scopes: offline_access openid profile email

with that setup, withPageAuthRequired would not redirect when token was expired and wouldn’t get an updated token. when I would use getAccessToken in an API route which was called from the page that had withPageAuthRequired, the getAccessToken function would error that the token was expired and couldn’t get refresh token.

I’ll try and get an example together for you by next Monday

@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 error

[AccessTokenError: The request to refresh the access token failed. CAUSE: invalid_grant (Unknown or invalid refresh token.)]

I tried using getSession in 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

  1. How do I fix this intermittent error when trying to retrieve an access token in the api route?
  2. Is it possible to modify the middleware auth so it confirms both a session and valid access token (otherwise redirecting to /api/auth/login)?