# SPA + API issue with JWT token

**URL:** <https://community.auth0.com/t/spa-api-issue-with-jwt-token/135086>\
**Category:** Get Help\
**Tags:** apis, application\
**Created:** [May 28, 2024, 6:51am UTC](https://community.auth0.com/t/spa-api-issue-with-jwt-token/135086 "2024-05-28T06:51:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexxx](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alexxx/32/49411_2.png) [@alexxx](https://community.auth0.com/u/alexxx)\
**Post date:** [May 28, 2024, 6:51am UTC](https://community.auth0.com/t/spa-api-issue-with-jwt-token/135086/1 "2024-05-28T06:51:57Z")

</div>

I have a simple React app + backend. I want to use an auth0 JWT to authorize API access in the backend.

I have read this tutorial, especially [SPA Angular 2 Implementation (SPAs + API)](https://auth0.com/docs/get-started/architecture-scenarios/spa-api/spa-implementation-angular2#5-call-the-api)

The issue is that I always get an opaque token, never a valid JWT so I can’t use it in the backend.

I have configured a single page application and an API in the Auth0 dashboard.

I get the token like this:

```auto
    const token = await getAccessTokenSilently({
      authorizationParams: {
        audience: `http://127.0.0.1:3100`,
      },
    });

```

I double checked that the audience is the same as set in the API in the dashboard. It doesn’t work, what am I missing here?

---

<div class="post-metadata">

**Author:** ![alexxx](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alexxx/32/49411_2.png) [@alexxx](https://community.auth0.com/u/alexxx)\
**Post date:** [May 28, 2024, 7:52pm UTC](https://community.auth0.com/t/spa-api-issue-with-jwt-token/135086/3 "2024-05-28T19:52:02Z")

</div>

I tried dozens of APIs and audiences including my “https://.auth0.com/” URL, and none returned an actual JWT. Please provide guidance.

---

<div class="post-metadata">

**Author:** ![alexxx](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alexxx/32/49411_2.png) [@alexxx](https://community.auth0.com/u/alexxx)\
**Post date:** [May 28, 2024, 8:09pm UTC](https://community.auth0.com/t/spa-api-issue-with-jwt-token/135086/4 "2024-05-28T20:09:52Z")

</div>

Even examples under here look like my code [auth0-react/examples/README.md at 6bf58e8b89f3ed49b1fbf701aee7d9088f4ccedb · auth0/auth0-react · GitHub](https://github.com/auth0/auth0-react/blob/6bf58e8b89f3ed49b1fbf701aee7d9088f4ccedb/examples/README.md)

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [May 28, 2024, 9:18pm UTC](https://community.auth0.com/t/spa-api-issue-with-jwt-token/135086/5 "2024-05-28T21:18:27Z")

</div>

Hey there @alexxx welcome to the community!

> [@alexxx](#):
>
> ```auto
> const token = await getAccessTokenSilently({
> authorizationParams: {
> audience: `http://127.0.0.1:3100`,
> },
> });
> 
> ```
> 
> I double checked that the audience is the same as set in the API in the dashboard. It doesn’t work, what am I missing here?

In your call to `getAccessTokenSilently` are you attempting to get a token for a different audience than specified previously? In `provideAuth0` for example:

```auto
import { bootstrapApplication } from '@angular/platform-browser';
import { provideAuth0 } from '@auth0/auth0-angular';
import { AppComponent } from './app.component';

bootstrapApplication(AppComponent, {
  providers: [
    provideAuth0({
      domain: '{yourDomain}',
      clientId: '{yourClientId}',
      authorizationParams: {
        redirect_uri: window.location.origin,
        audience: '{your_api_audience}'
      }
    }),
  ]
});

```

Here is our [Angular quickstart](https://auth0.com/docs/quickstart/spa/angular/01-login#register-and-providing-auth0) for reference.

---

<div class="post-metadata">

**Author:** ![alexxx](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alexxx/32/49411_2.png) [@alexxx](https://community.auth0.com/u/alexxx)\
**Post date:** [May 28, 2024, 11:55pm UTC](https://community.auth0.com/t/spa-api-issue-with-jwt-token/135086/6 "2024-05-28T23:55:35Z")

</div>

Thanks!

I’m requesting the same audience:

```tsx
import {Auth0Provider, withAuthenticationRequired} from '@auth0/auth0-react';
// ...
      <Auth0Provider
      domain={AUTH0_DOMAIN}
      clientId={AUTH0_CLIENT_ID}
      onRedirectCallback={onRedirectCallback}
      authorizationParams={{
        audience: 'http://127.0.0.1:3100',
      }}
      {...props}
    >
      <Outlet />
    </Auth0Provider>

```

I’ve already looked at the Angular tutorial and seems to do the same things.

---

<div class="post-metadata">

**Author:** ![alexxx](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alexxx/32/49411_2.png) [@alexxx](https://community.auth0.com/u/alexxx)\
**Post date:** [May 29, 2024, 2:40am UTC](https://community.auth0.com/t/spa-api-issue-with-jwt-token/135086/7 "2024-05-29T02:40:15Z")

</div>

The odd thing is I get `ApolloError: Login required` and a redirect when I run the `getAccessTokenSilently` part. So it feels like it’s not using the session I created by logging in.

It redirects my app to `/authorize` and I can see the request contains the right audience in the parameters.

---

<div class="post-metadata">

**Author:** ![alexxx](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alexxx/32/49411_2.png) [@alexxx](https://community.auth0.com/u/alexxx)\
**Post date:** [May 29, 2024, 3:21am UTC](https://community.auth0.com/t/spa-api-issue-with-jwt-token/135086/8 "2024-05-29T03:21:41Z")

</div>

Found the bug 🤦‍♂️ in the code above, `props` from a higher level component was overriding my `authorizationParams`.

One more question: do my users have to register and then also accept that OAuth prompt to share email/name? It sounds like a duplicated UX and more attrition for the signup.

---

<div class="post-metadata">

**Author:** ![alexxx](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alexxx/32/49411_2.png) [@alexxx](https://community.auth0.com/u/alexxx)\
**Post date:** [June 4, 2024, 3:42am UTC](https://community.auth0.com/t/spa-api-issue-with-jwt-token/135086/9 "2024-06-04T03:42:59Z")

</div>

@ty.frith did you have any opinion about the above question? Thanks!

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [June 5, 2024, 7:03pm UTC](https://community.auth0.com/t/spa-api-issue-with-jwt-token/135086/10 "2024-06-05T19:03:30Z")

</div>

> [@alexxx](#):
>
> One more question: do my users have to register and then also accept that OAuth prompt to share email/name? It sounds like a duplicated UX and more attrition for the signup.

That’s correct - Unless the application is first-party with `Allow Skipping User Consent` setting enabled:

> **[User Consent and Third-Party Applications](https://auth0.com/docs/get-started/applications/confidential-and-public-applications/user-consent-and-third-party-applications#skip-consent-for-first-party-applications)**
>
> Learn how to decouple APIs from applications that consume them and define third-party apps that you don't control or may not trust.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [June 19, 2024, 7:04pm UTC](https://community.auth0.com/t/spa-api-issue-with-jwt-token/135086/11 "2024-06-19T19:04:18Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
