With nextjs-auth0, how can I get an access token with a different audience than I passed during the login?

Problem statement

When using the getAccessToken function, I need to pass an audience but it doesn’t seem like I can add this dynamically like in auth0-react. It appears that we are stuck with the single audience that was provided after the user logs in. Is there a way to use getAccessToken to get an access token with a different audience than what was used on the original login?

Solution

The function getAccessToken uses the refresh grant - so you can’t update the audience on the access token using this. You would need to log in again to get an access token for a new audience (which you can do using the SDK).

One of our engineers has implemented a workaround for a similar requirement here: https://github.com/Danco626/cic-nextjs-session/blob/main/pages/api/auth/[...auth0].js

However, please note that this code is just to be used as an example and should be tested thoroughly before you push it to production.

1 Like