nextjs-auth0: Refresh token error when calling getAccessToken in v4 beta

Checklist

Description

Getting this error while testing out an upgrade to Next 15 + Auth0 v4. Haven’t changed anything about the access/refresh token settings from v3 and need some guidance on how to debug further.

{"type":"AccessTokenError","message":"The access token has expired and there was an error while trying to refresh it. Check the server logs for more information.","stack":"AccessTokenError: The access token has expired and there was an error while trying to refresh it. Check the server logs for more information.\n    at Auth0Client.getAccessToken (/.next-dev/server/chunks/ssr/node_modules_47bd98._.js:6483:19)\n    at async getAuthorizationHeader (next-dev/server/chunks/ssr/src_82b8b1._.js:963:24)\n    at async /.next-dev/server/chunks/ssr/src_82b8b1._.js:1266:31","name":"AccessTokenError","code":"failed_to_refresh_token"},"msg":"Error retrieving access token"}

Reproduction

Here’s my auth0 client initialization:

export const auth0 = new Auth0Client({
  onCallback: async (error, context) => {
    const { public: publicConfig } = getFullConfig();
    if (error != null) {
      return handleCallbackError(error.code, error.message);
    }
    return NextResponse.redirect(
      new URL(context.returnTo || '/', publicConfig.baseURL),
    );
  },
  authorizationParameters: {
    scope: 'openid profile email offline_access',
    audience: 'https://api.<my_domain>.com',
  },
});

Looks like the initial access token is valid but then after expiration I get the error above. After inspecting the session it looks like a refresh token is present, but not sure why it fails to generate an access token

Additional context

No response

nextjs-auth0 version

4.0.0-beta.10

Next.js version

15.1

Node.js version

20.9.0

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19 (7 by maintainers)

Most upvoted comments

Could that be avoided using AsyncLocalStorage though?

If you have a signature like this

export default withMiddlewareAuthRequired(middlewareFunction)

Then the wrapper can establish a context that holds the latest session info for the duration of the callstack (including the inner middlewareFunction). Then any call to getAccessToken() inside of that context should be able to access the latest token in case it was refreshed, and the final session result can be automatically applied to whatever response is returned from middlewareFunction. It would also let you return to automatically refreshing the token in the middleware instead of the end user needing to ensure they are always calling getAccessToken in the middleware in order to keep tokens refreshed.