# SPAs and API calls -- how do i determine my user

**URL:** <https://community.auth0.com/t/spas-and-api-calls-how-do-i-determine-my-user/62752>\
**Category:** Get Help\
**Created:** [May 13, 2021, 5:01pm UTC](https://community.auth0.com/t/spas-and-api-calls-how-do-i-determine-my-user/62752 "2021-05-13T17:01:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kagemusha](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kagemusha/32/29396_2.png) [@kagemusha](https://community.auth0.com/u/kagemusha)\
**Post date:** [May 13, 2021, 5:01pm UTC](https://community.auth0.com/t/spas-and-api-calls-how-do-i-determine-my-user/62752/1 "2021-05-13T17:01:40Z")

</div>

Most apps are going to need to identify a user so that the user has access to their own stuff but not others. E.g. a blogging platform which allows users to edit their own posts but not others. How do i get my user from the auth0 token?

Problem is essentially same as here: [How to model entity relationships when user identities are managed by Auth0?](https://community.auth0.com/t/how-to-model-entity-relationships-when-user-identities-are-managed-by-auth0/6606)

I understand the general approach, but a tutorial or github repo with both the SPA side and the API side would be really helpful, e.g. how do i send my auth0\_id (or similar) with my request so that i can use that to determine user on the API side.

---

<div class="post-metadata">

**Author:** ![stephanie.chamblee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stephanie.chamblee/32/26087_2.png) [@stephanie.chamblee](https://community.auth0.com/u/stephanie.chamblee)\
**Post date:** [May 13, 2021, 6:21pm UTC](https://community.auth0.com/t/spas-and-api-calls-how-do-i-determine-my-user/62752/3 "2021-05-13T18:21:10Z")

</div>

Hi @kagemusha,

You can find example applications that demonstrate calling an API in our Quickstarts:

> **[Auth0 Single-Page App Quickstarts](https://auth0.com/docs/quickstart/spa)**
>
> Browse single-page app quickstarts to learn how to quickly add authentication to your app.

Auth0’s [SPA SDK](https://auth0.com/docs/libraries/auth0-single-page-app-sdk) follows an [OAuth2](https://auth0.com/docs/videos/learn-identity-series/openid-connect-and-oauth2) flow called [Authorization Code Flow with Proof Key for Code Exchange](https://auth0.com/docs/flows/authorization-code-flow-with-proof-key-for-code-exchange-pkce). In this flow, after a user logins, Auth0 will issue the SPA an ID Token and an Access Token:

- ID Token - This token is used by the SPA to get basic profile information. It is received as a JWT and conveniently decoded by whichever Auth0 SDK you are using. For example with `auth0-spa-js`:

```auto
document.getElementById('login').addEventListener('click', () => {
  auth0.loginWithRedirect({
    redirect_uri: 'http://localhost:3000/'
  }).then(token => {
    //logged in. you can get the user profile like this:
    auth0.getUser().then(user => {
      console.log(user);
    });
  });
});

```

- Access Token - The SPA will pass this token in its Authorization header as a bearer token in API requests:

```auto
document.getElementById('callApi').addEventListener('click', () => {
  auth0
    .getTokenSilently()
    .then(accessToken =>
      fetch('https://exampleco.com/api', {
        method: 'GET',
        headers: {
          Authorization: 'Bearer ' + accessToken
        }
      })
    )
    .then(result => result.json())
    .then(data => {
      console.log(data);
    });
});

```

Since it sounds like you may be calling your own API, you will need to [register your API](https://auth0.com/docs/get-started/set-up-apis) with Auth0 and use the API identifier as the audience when you initiate the Auth0 client:

```auto

import createAuth0Client from '@auth0/auth0-spa-js';

// either with async/await
const auth0 = await createAuth0Client({
  domain: 'YOUR_DOMAIN',
  client_id: 'YOUR_CLIENT_ID',
  audience: 'YOUR_API_IDENTIFIER'
});

```

For a more comprehensive walkthrough, I’d recommend taking a look at the Auth0 blog for articles such as:

- [The Complete Guide to React User Authentication with Auth0](https://auth0.com/blog/complete-guide-to-react-user-authentication/)
- [The Complete Guide to Angular User Authentication with Auth0](https://auth0.com/blog/complete-guide-to-angular-user-authentication/)
- [The Complete Guide to Vue.js User Authentication with Auth0](https://auth0.com/blog/complete-guide-to-vue-user-authentication/)

---

<div class="post-metadata">

**Author:** ![kagemusha](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kagemusha/32/29396_2.png) [@kagemusha](https://community.auth0.com/u/kagemusha)\
**Post date:** [May 13, 2021, 8:42pm UTC](https://community.auth0.com/t/spas-and-api-calls-how-do-i-determine-my-user/62752/4 "2021-05-13T20:42:33Z")

</div>

is the access token a JWT also? do I include the identifying info there (e.g. auth0\_id)? if so how?

and then I assume I decode the token on the API server call to get the id? If you have a Rails snippet (or some other server-side code) on how to do this, would be helpful

---

<div class="post-metadata">

**Author:** ![stephanie.chamblee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stephanie.chamblee/32/26087_2.png) [@stephanie.chamblee](https://community.auth0.com/u/stephanie.chamblee)\
**Post date:** [May 14, 2021, 1:22pm UTC](https://community.auth0.com/t/spas-and-api-calls-how-do-i-determine-my-user/62752/5 "2021-05-14T13:22:46Z")

</div>

Hi @kagemusha,

The Access Token will be a JWT if you provide an audience in the authorization request. For example:

```auto
const auth0 = await createAuth0Client({
  domain: 'YOUR_DOMAIN',
  client_id: 'YOUR_CLIENT_ID',
  audience: 'YOUR_API_IDENTIFIER' // <-- use your API identifier as the audience
});

```

Otherwise, it will be an opaque token that can’t be decoded, but can be used for calling the `https://your-domain/userinfo` endpoint.

> [@kagemusha](#):
>
> do I include the identifying info there (e.g. auth0\_id)?

When you provide an audience in your SPA application and receive a JWT for the Access Token, the `sub` claim of the decoded token will be the user ID within Auth0. You can use this to retrieve additional information about the user.

> [@kagemusha](#):
>
> If you have a Rails snippet (or some other server-side code) on how to do this, would be helpful

For an example of using the Ruby on Rails SDK, you can check out the Quickstart:  
[https://auth0.com/docs/quickstart/webapp/rails/01-login](https://auth0.com/docs/quickstart/webapp/rails/01-login)

---

<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:** [May 29, 2021, 1:22pm UTC](https://community.auth0.com/t/spas-and-api-calls-how-do-i-determine-my-user/62752/6 "2021-05-29T13:22:48Z")

</div>

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