# How to secure an SPA which fetch data from REST api? Does every authentication needs to be done at backend?

**URL:** <https://community.auth0.com/t/how-to-secure-an-spa-which-fetch-data-from-rest-api-does-every-authentication-needs-to-be-done-at-backend/57780>\
**Category:** Get Help\
**Tags:** jwt, api, login\
**Created:** [February 12, 2021, 1:56pm UTC](https://community.auth0.com/t/how-to-secure-an-spa-which-fetch-data-from-rest-api-does-every-authentication-needs-to-be-done-at-backend/57780 "2021-02-12T13:56:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PTanwarCS](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ptanwarcs/32/27572_2.png) [@PTanwarCS](https://community.auth0.com/u/PTanwarCS)\
**Post date:** [February 12, 2021, 1:56pm UTC](https://community.auth0.com/t/how-to-secure-an-spa-which-fetch-data-from-rest-api-does-every-authentication-needs-to-be-done-at-backend/57780/1 "2021-02-12T13:56:11Z")

</div>

I am having spring-boot powered REST Apis which are used by my Single Page Application build using React.js.

I now want to add authentication layer to it. I am already using JWT for it but I need to replace it with Auth0. But I am very much confused on how the flow should be like. All the API endpoints are secured and only authenticated user have access to them.

Since Auth0 also provide browser support where everything from login, signup redirect etc are handled out of the box my Auth0, should I use IDP directly into my browser client or should the backend(REST) handle these?  
Every request to the backend should be validated for authentication.

---

<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:** [February 16, 2021, 2:06pm UTC](https://community.auth0.com/t/how-to-secure-an-spa-which-fetch-data-from-rest-api-does-every-authentication-needs-to-be-done-at-backend/57780/3 "2021-02-16T14:06:57Z")

</div>

Hi @PTanwarCS,

Typically with a SPA, authentication looks like this:

1. The user logs into Auth0 which acts as the authorization server (e.g. Universal Login)
2. Auth0 redirects the user back to the SPA along with a code
3. The SPA requests tokens using the code
4. Auth0 issues two tokens, an ID Token and an Access Token

This process is simplified with Auth0 SDKs such as the React SDK. If you haven’t checked it out already, you may want to take a look at the [React Quickstart](https://auth0.com/docs/quickstart/spa/react/01-login) or the [Complete Guide to React Authentication](https://auth0.com/blog/complete-guide-to-react-user-authentication/)

The React app can use the ID Token to find data on the user such as their name, email, etc. The Access Token is used for calling an API.

When you [register your API with Auth0](https://auth0.com/docs/get-started/set-up-apis) and provide an `audience` when you initialize the `Auth0Provider`, then Auth0 will issue you a JWT Access Token that can be used with your API as a bearer token. Your API can [validate the JWT Access Token](https://auth0.com/docs/tokens/guides/validate-access-tokens#custom-api-access-tokens) to ensure it was issued by Auth0.

```auto
ReactDOM.render(
  <Auth0Provider
    domain={config.domain}
    clientId={config.clientId}
    audience={config.audience}
    redirectUri={window.location.origin}
  >
    <App />
  </Auth0Provider>,
  document.getElementById("root")
);

```

Since it sounds like you have multiple APIs and you can only pass one audience to the `Auth0Provider`, you may want to look into this solution: [Configure Logical API for Multiple APIs](https://auth0.com/docs/authorization/represent-multiple-apis-using-a-single-logical-api?_ga=2.152698618.239155659.1613392532-1595415333.1607347674)

Here is an architecture scenario that may be helpful for a SPA + API: [Single-Page Applications (SPA) with API](https://auth0.com/docs/architecture-scenarios/spa-api)

---

<div class="post-metadata">

**Author:** ![PTanwarCS](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ptanwarcs/32/27572_2.png) [@PTanwarCS](https://community.auth0.com/u/PTanwarCS)\
**Post date:** [February 22, 2021, 10:58am UTC](https://community.auth0.com/t/how-to-secure-an-spa-which-fetch-data-from-rest-api-does-every-authentication-needs-to-be-done-at-backend/57780/4 "2021-02-22T10:58:04Z")

</div>

Thanks for your response.  
Shouldn’t the backend API be responsible for connecting with Auth0? I don’t want to redirect to the Auth0 login page. In that case everything should be handled by my backend API, shouldn’t it?

---

<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:** [February 22, 2021, 8:34pm UTC](https://community.auth0.com/t/how-to-secure-an-spa-which-fetch-data-from-rest-api-does-every-authentication-needs-to-be-done-at-backend/57780/5 "2021-02-22T20:34:18Z")

</div>

[Universal Login](https://auth0.com/docs/universal-login) is the recommended approach, but you can configure an embedded login: [Embedded Login](https://auth0.com/docs/login/embedded-login)

Note: Browsers such as Safari block third-party cookies, so you will need to use a [Custom Domain](https://auth0.com/docs/custom-domains) to manage CORS issues.

Your API can handle the authentication if the SPA meets the following criteria:

- It is served to the client using your own backend.
- It has the same domain as your backend.
- It makes API calls that require authentication to your backend.

Here is the full documentation: [Authenticate Single-Page Apps With Cookies](https://auth0.com/docs/sessions-and-cookies/spa-authenticate-with-cookies)

---

<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:** [March 9, 2021, 8:34pm UTC](https://community.auth0.com/t/how-to-secure-an-spa-which-fetch-data-from-rest-api-does-every-authentication-needs-to-be-done-at-backend/57780/6 "2021-03-09T20:34:24Z")

</div>

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