# How Authentication and Authorization Work for SPAs

**URL:** <https://devforum.okta.com/t/how-authentication-and-authorization-work-for-spas/23861>\
**Category:** Developer Blog Comments\
**Created:** [April 4, 2023, 2:59pm UTC](https://devforum.okta.com/t/how-authentication-and-authorization-work-for-spas/23861 "2023-04-04T14:59:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [April 4, 2023, 2:59pm UTC](https://devforum.okta.com/t/how-authentication-and-authorization-work-for-spas/23861/1 "2023-04-04T14:59:37Z")

</div>

Adding authentication to public clients such as Single Page Applications (SPA) and JavaScript applications can be a source of confusion. Identity Providers like Okta try to help you via multiple support systems. Still, it can feel like a lot of work. Especially since you’re responsible for way more than authentication alone in the applications you work on!

* * *
This is a companion discussion topic for the original entry at [https://developer.okta.com/blog/2023/04/04/spa-auth-tokens](https://developer.okta.com/blog/2023/04/04/spa-auth-tokens)

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [January 31, 2024, 11:11am UTC](https://devforum.okta.com/t/how-authentication-and-authorization-work-for-spas/23861/3 "2024-01-31T11:11:43Z")

</div>

Listed on January 31, 2024, 11:11am UTC



---

<div class="post-metadata">

**Author:** ![Mahoney266](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/mahoney266/32/5463_2.png) [@Mahoney266](https://devforum.okta.com/u/Mahoney266)\
**Post date:** [April 29, 2024, 1:43pm UTC](https://devforum.okta.com/t/how-authentication-and-authorization-work-for-spas/23861/4 "2024-04-29T13:43:59Z")

</div>

I don’t understand from this how to validate the `aud` claim on the access token in my API.

e.g. I have an SPA served on `app.example.com`.

I have a JSON/HTTP API served on `api.example.com`.

I have configured my SPA to discover from `https://trial-12345.okta.com` - so it is hitting `https://trial-12345.okta.com/.well-known/openid-configuration` as a start point.

After successful authentication my SPA gets an `access_token` field containing `"aud": "https://trial-12345.okta.com"`.

I pass that as `Authorization: Bearer <access_token>` when calling `https://api.example.com`. But how do I validate it server side? Do I have to make my server side app happy to accept `https://trial-12345.okta.com` as the `aud`? That seems wrong, surely it should be `https://api.example.com` as the `aud`?

I can change the default Authorization Server in Settings → Security → API to have an Audience of `https://api.example.com` - but that makes no difference to the `aud` claim returned in the `access_token` when doing the OIDC flow. I suspect because I am getting the metadata from `https://trial-12345.okta.com/.well-known/*` rather than `https://trial-12345.okta.com/oauth2/default/.well-known/*`. Am I right in this?

I find it very frustrating that this is so opaque and obscure when getting an access token to use with an API must be the absolutely standard use case, and validating the `aud` claim of an oauth2 acces token is meant to be a critical security action?
