Next.js using getSession or getAccessToken resulting in user authentication being lost

Hi @kat1

Welcome to the Auth0 Community

From what i have checked there doesn’t seem to be a particular issue with your code. However as mentioned in the NextJs SDK as well :

// getAccessToken will fetch you a new one using the refresh_token grant

So the possible solution for using both getSession() or getAccessToken() in a server-side environment would be to :

  • Include the offline_access scope in the configuration (or AUTH0_SCOPE).
  • Check “Allow Offline Access” in the API Settings.
  • Make sure the “Refresh Token” grant is enabled in the Application Settings (this is the default).

This information is outlined in one of our Knowledge Articles as well.

You can also check this github repository for examples of this implementation here.

You can let us know if this did the trick for you and I hope this helped,
Remus