Getting logged out (and thus a No Auth Token Found) error when trying to generate a token?

Greetings! I’m trying to work with React (and the Auth0 React Hook) to access a protected endpoint that I’ve set up locally (for testing/learning purposes).

I’ve got the following function defined in my React component:

const getToken = async () => {
        try {
            const token = await getAccessTokenSilently({
                audience: 'https://codestore.fourthspacegames.com/api',
                scope: 'read:test'
            });
            console.log(token);
        } catch (err) {
            console.error(err);
        }
    }

Where the audience is set to properly match the API I’m working attempting to access, and the scope is theoretically a scope attached to the Admin role that my current logged in user has.

The goal at this point is to just click a button and call getAccessTokenSilently() to generate the token that I’d need to send to my server (via Bearer?) to make a request to the protected API.

However, when I click said button, it seems to log me out of the app I’m testing it with and therefore throws an error that I’m not logged in and nothing can be generated.

What am I doing wrong?

I figured out what was happening.

It didn’t like a different audience being configured, I thought I had to set it in the event handler.

I set the audience in the Provider instead and removed the options from the call to the hook and it’s fine now.

Oops.

1 Like

Perfect! Glad you have figured it out!