# How to securely access custom backend API after SPA authentication?

**URL:** <https://community.auth0.com/t/how-to-securely-access-custom-backend-api-after-spa-authentication/106447>\
**Category:** Get Help\
**Tags:** classic-universal-login-experience, login-experience\
**Created:** [May 8, 2023, 9:27pm UTC](https://community.auth0.com/t/how-to-securely-access-custom-backend-api-after-spa-authentication/106447 "2023-05-08T21:27:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![idatarbel](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/idatarbel/32/46392_2.png) [@idatarbel](https://community.auth0.com/u/idatarbel)\
**Post date:** [May 8, 2023, 9:27pm UTC](https://community.auth0.com/t/how-to-securely-access-custom-backend-api-after-spa-authentication/106447/1 "2023-05-08T21:27:35Z")

</div>

Hello,

I am attempting to create a single-page ReactJs application that prompts my user to log in using the Auth0 Universal Login page. Once the user is logged into my React app, the app needs to call my custom NodeJS API which will do two things:

1. Perform Management API calls in order to generate a user management screen.
2. Perform simple CRUD operations on a custom database.

I am starting with the ReactJS quickstart app. I am trying to understand how to extend it to securely call my custom backend API. I was hoping that I could simply pass the access token generated by the quickstart app to my API as a bearer token and that my API could simply validate it and then perform the requested actions. But, I am not sure this is possible.

Bottom Line: Once my user is authenticated by my ReactApp, how do I enable the React app to access my backend API? And, how do I prevent anyone not authorized by my ReactApp to access my backend API?

---

<div class="post-metadata">

**Author:** ![phi1ipp](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/phi1ipp/32/40616_2.png) [@phi1ipp](https://community.auth0.com/u/phi1ipp)\
**Post date:** [May 8, 2023, 11:55pm UTC](https://community.auth0.com/t/how-to-securely-access-custom-backend-api-after-spa-authentication/106447/3 "2023-05-08T23:55:05Z")

</div>

Your idea is absolutely correct, you need to get an access\_token in the frontend and send it to your backend, which will validate it and do whatever is required

---

<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 9, 2023, 12:07am UTC](https://community.auth0.com/t/how-to-securely-access-custom-backend-api-after-spa-authentication/106447/4 "2023-05-09T00:07:16Z")

</div>

Hey there @idatarbel and thanks for the input @phi1ipp!

I recommend checking out the [node-oauth2-jwt-bearer](https://github.com/auth0/node-oauth2-jwt-bearer/tree/main/packages/express-oauth2-jwt-bearer) library outlined here:

> **[Introducing the OAuth 2.0 Express SDK for Protecting APIs with JWT Bearer Tokens](https://auth0.com/blog/introducing-oauth2-express-sdk-protecting-api-with-jwt/)**
>
> Learn how the OAuth 2.0 Express SDK released by Auth0 makes protecting your Express API easier.

For using the Management API in particular, you’ll want to take a look at the [ManagementClient](https://github.com/auth0/node-auth0#management-api-client) of node-auth0.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![idatarbel](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/idatarbel/32/46392_2.png) [@idatarbel](https://community.auth0.com/u/idatarbel)\
**Post date:** [May 9, 2023, 12:12am UTC](https://community.auth0.com/t/how-to-securely-access-custom-backend-api-after-spa-authentication/106447/5 "2023-05-09T00:12:02Z")

</div>

Great.

1. Can you point me to example access token validation code for NodeJs?

2. And, what if anything beyond what I set up to run the React quick start app do I need to set up in the Auth0 dashboard to get this API communication to work? For instance, do I need a separate machine to machine API? Do I need a separate custom auth server or something?

---

<div class="post-metadata">

**Author:** ![idatarbel](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/idatarbel/32/46392_2.png) [@idatarbel](https://community.auth0.com/u/idatarbel)\
**Post date:** [May 9, 2023, 5:06pm UTC](https://community.auth0.com/t/how-to-securely-access-custom-backend-api-after-spa-authentication/106447/6 "2023-05-09T17:06:04Z")

</div>

Sorry, I somehow missed [tyf](https://community.auth0.com/u/tyf)’s response. The suggestion to use [node-oauth2-jwt-bearer](https://github.com/auth0/node-oauth2-jwt-bearer/tree/main/packages/express-oauth2-jwt-bearer) worked.

For those who face the same question I did (how to call a custom backend API after your user has been authenticated), here’s the NodeJs API code you need:

```auto
//I use import instead of require because I have other dependencies in my express server that necessitate import statements:
import { auth } from 'express-oauth2-jwt-bearer';

//Next, I get values for two variables from the Auth0 dashboard's Application section. Note that 
//you have to preface variables with "REACT_APP_" when you put them in a .env file or ReactJs 
//won't be able to locate them.
const auth0IssuerBaseUrl= process.env.REACT_APP_AUTH0_ISSUER_BASE_URL;
const auth0Audience = process.env.REACT_APP_AUTH0_AUDIENCE;

// Next, make sure you set cors as follows or you will get cors errors when your front end calls your API:
app.use(cors({
    origin: '*'
}));

//Next, plug in this access token validation code. Make sure this code comes AFTER the cors 
//code or this code will generate a cors error.
app.use( auth({
    issuerBaseURL: auth0IssuerBaseUrl,
    audience: auth0Audience
  }));

```

Here is a test method from the ReactJs front end app:

```auto
const callApi = async () => {
    try {
      //This will work:
      let token = await getAccessTokenSilently();

      //This won't work -- which is expected -- because it is an invalid token.
      //let token = "Doesn't work";

      const response = await fetch(`${apiOrigin}/api/private`, {
        headers: {
          'content-type': 'application/json',
          accept: 'application/json',
          Authorization: `Bearer ${token}`,
        },
      });

```

…

---

<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 9, 2023, 5:39pm UTC](https://community.auth0.com/t/how-to-securely-access-custom-backend-api-after-spa-authentication/106447/7 "2023-05-09T17:39:05Z")

</div>

That’s great I’m glad to hear the library worked for you, and thanks for sharing with the community! 🚀

---

<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 23, 2023, 5:39pm UTC](https://community.auth0.com/t/how-to-securely-access-custom-backend-api-after-spa-authentication/106447/8 "2023-05-23T17:39:51Z")

</div>

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