auth0 / auth0-angular

Auth0 SDK for Angular Single Page Applications
MIT License
176 stars 58 forks source link

SDK should handle getting a new token based on the interceptor configuration provided. #594

Closed mharrisn closed 3 months ago

mharrisn commented 6 months ago

Checklist

Describe the problem you'd like to have solved

Hello, I assume a feature request is most appropriate for this question. I have an app that needs to call two different APIs. Both APIs have their audience defined in Auth0.

Assuming we have primary-api-audience-identifier as our default audience in AuthClientConfig, I was expecting that if I had an Interceptor configuration like this:

httpInterceptor: {
      allowedList: [
        {
          uriMatcher: (uri) => {
             return  uri.toLowerCase().includes('other-api');
          },
           tokenOptions: {
            authorizationParams : {
              audience: 'other-api-audience-identifier'
            }
          }
        }
      ]
    }

The SDK would get a new Access token if it needed one for the audience specified in tokenOptions. But that doesn't seem to be the case. Is there another suggested way to handle this use case? The only thing that I could find that sounded similar was this closed Issue: https://github.com/auth0/auth0-angular/issues/214

It's not clear to me what the point of the audience is here if the SDK doesn't use it to grab a token.

Describe the ideal solution

The SDK checks whether there is a cached token with the specified audience, and gets an access token as needed (the same logic that's currently used for pulling access tokens silently).

Alternatives and current workarounds

I assume if I manually call AuthService.getTokenSilently, that would provide the token. But this seems like something the SDK should handle automatically to keep the responsibility of managing tokens in the SDK.

Additional context

No response

frederikprijck commented 6 months ago

Hello.

The SDK checks whether there is a cached token with the specified audience, and gets an access token as needed (the same logic that's currently used for pulling access tokens silently).

This is how the SDK works, or is intended to work at least.

As you can see here, our interceptor calls getTokenSilently, which does exactly what you want. You can also see that we take the tokenOptions from the interceptor config and use that as the options to pass down to getTokenSilently.

If this does not work for you, can you share a reproduction and we can help troubleshoot what's going on. You can use our sample application to reproduce and share.

mharrisn commented 6 months ago

I can't use any of my organization's tenants for a reproduction case. Do you have any suggestions on how I can define APIs for reproduction? Is there a sample tenant - like the sample application that I could use as a starter?

frederikprijck commented 6 months ago

You can create a different tenant for yourself, it's free.

frederikprijck commented 3 months ago

Closing due to inactivity, feel free to let us know if you have a reproduction and still need us to look into it, can reopen as needed.