# Securing NodeJS Backend as well as my React-Frontend (using Auth0-Lock)?

**URL:** <https://community.auth0.com/t/securing-nodejs-backend-as-well-as-my-react-frontend-using-auth0-lock/53647>\
**Category:** Get Help\
**Created:** [November 21, 2020, 2:04pm UTC](https://community.auth0.com/t/securing-nodejs-backend-as-well-as-my-react-frontend-using-auth0-lock/53647 "2020-11-21T14:04:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![johncat](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/johncat/32/12826_2.png) [@johncat](https://community.auth0.com/u/johncat)\
**Post date:** [November 21, 2020, 2:04pm UTC](https://community.auth0.com/t/securing-nodejs-backend-as-well-as-my-react-frontend-using-auth0-lock/53647/1 "2020-11-21T14:04:34Z")

</div>

Beginner here & new to this. I have a simple subscription site (gatsbyjs), & I am using the Auth0 Lock to enable users access to my site. My site queries some data from my nodejs server via graphql.

Now I realised that while my frontend is secured by auth0, my backend isn’t. A malicious user could thus theoretically send queries to my graphql backend en masse, even if he/she is not logged in via the frontend.

How would I go about securing the backend now? I only want logged in users to be able to query data. However, authentication goes via auth0 & the lock api, so my server seems kind of separate of all of this?

Any advice?

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [November 23, 2020, 4:01pm UTC](https://community.auth0.com/t/securing-nodejs-backend-as-well-as-my-react-frontend-using-auth0-lock/53647/3 "2020-11-23T16:01:32Z")

</div>

Hi @johncat,

Welcome to the Community!

You can control access to a backend API with access tokens. See this article:

> **[Call Your API from Your Single-Page App](https://auth0.com/docs/microsites/call-api/call-api-single-page-app)**
>
> Everything you need to know to call your API from your single-page app (SPA)

---

<div class="post-metadata">

**Author:** ![johncat](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/johncat/32/12826_2.png) [@johncat](https://community.auth0.com/u/johncat)\
**Post date:** [November 26, 2020, 1:39pm UTC](https://community.auth0.com/t/securing-nodejs-backend-as-well-as-my-react-frontend-using-auth0-lock/53647/4 "2020-11-26T13:39:47Z")

</div>

Thanks a lot for the reply! Just to make sure I understand this correctly, let me paraphrase this in absolute beginner terms:

My React frontend SPA makes a request to the Auth0 server via Lock.  
The Auth0 server replies with a id-token & an access token.

Now I take the access token & include it in my request to my node server. The node server grabs the access token & itself sends a request to the Auth0 server. Auth0 responds and says: Yes, that’s a valid token. And then (then & only then) my node server sends back the requested data to the frontend.

Is this correct?

Also, and relatedly, is it ok for my SPA to store access token & id token in state (React state for example)? Or is there something to worry about?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![johncat](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/johncat/32/12826_2.png) [@johncat](https://community.auth0.com/u/johncat)\
**Post date:** [November 26, 2020, 6:27pm UTC](https://community.auth0.com/t/securing-nodejs-backend-as-well-as-my-react-frontend-using-auth0-lock/53647/5 "2020-11-26T18:27:57Z")

</div>

So I just tried it the way I describe above, with a simple test route at `/accesstoken`. I take the access token I get in my SPA, send it to my node server, and make a request to Auth0. It’s not working so far.

I send the token via post request to my server:

> app.post(‘/accesstoken’, async function (req, res) {  
> const { token } = req.body; //grabbing the access token from the frontend
> 
> ```
> const user = await doesUserExist({ //this is a simple function which works when I request the token directly from the server with my client id & secret; using the token from the SPA, via req.body, does not work though.
> email: 'someuser@test.com',
> token,
> });
> 
> res.send({ doesUserExist: userExistsInAuth0 ? true : false });
> 
> ```
> 
> });

However, I am getting a `status: 401,`, `statusText: 'Unauthorized',`. When requesting directly via the server it works. The audience field is the same in both SPA & node server (is this how it should be? not sure, but with a different audience field it didn’t work).

Not sure where I am going wrong here?

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [November 30, 2020, 5:24pm UTC](https://community.auth0.com/t/securing-nodejs-backend-as-well-as-my-react-frontend-using-auth0-lock/53647/6 "2020-11-30T17:24:36Z")

</div>

> [@johncat](#):
>
> The node server grabs the access token & itself sends a request to the Auth0 server. Auth0 responds and says: Yes, that’s a valid token. And then (then & only then) my node server sends back the requested data to the frontend.

You can avoid this second call to the Auth0 server. The token is a stateless bearer token, which means it is signed, and can be verified without any additional call. You backend API can verify the token, then return the data.

Take a look at our [node quickstart](https://auth0.com/docs/quickstart/backend/nodejs), it shows how to validate tokens on the backend.

---

<div class="post-metadata">

**Author:** ![johncat](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/johncat/32/12826_2.png) [@johncat](https://community.auth0.com/u/johncat)\
**Post date:** [December 2, 2020, 10:42pm UTC](https://community.auth0.com/t/securing-nodejs-backend-as-well-as-my-react-frontend-using-auth0-lock/53647/7 "2020-12-02T22:42:30Z")

</div>

Thank you so much, I think I understand this better now! 🙂

I have one last question though. Is it ok for me to just save the access token in state (via React Context) or localstorage in my frontend? Or is this generally not advisable?

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [December 2, 2020, 10:44pm UTC](https://community.auth0.com/t/securing-nodejs-backend-as-well-as-my-react-frontend-using-auth0-lock/53647/8 "2020-12-02T22:44:50Z")

</div>

We don’t recommend keeping access tokens in localstorage due to the possibility of XSS attacks. The Auth0 React SDK should be handling all of that for you, and I would highly recommend it vs implementing yourself.

---

<div class="post-metadata">

**Author:** ![johncat](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/johncat/32/12826_2.png) [@johncat](https://community.auth0.com/u/johncat)\
**Post date:** [December 2, 2020, 10:46pm UTC](https://community.auth0.com/t/securing-nodejs-backend-as-well-as-my-react-frontend-using-auth0-lock/53647/9 "2020-12-02T22:46:37Z")

</div>

Ok, thank you. I am using `gatsby-theme-auth0` ([GitHub - epilande/gatsby-theme-auth0: 🔐 A Gatsby Theme for adding Auth0 to your application.](https://github.com/epilande/gatsby-theme-auth0) /  
[gatsby-theme-auth0 | Gatsby](https://www.gatsbyjs.com/plugins/gatsby-theme-auth0/)) right now.

If you happen to know this package, let me know what you / the Auth0 team thinks about it.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [December 3, 2020, 4:20pm UTC](https://community.auth0.com/t/securing-nodejs-backend-as-well-as-my-react-frontend-using-auth0-lock/53647/10 "2020-12-03T16:20:47Z")

</div>

I’m not familiar with it. It looks like it’s built on auth0.js, which is our older sdk. We have sample apps for gatsby that use our newer react-specific SDK. Check this out:

> **[auth0-react/examples/gatsby-app at master · auth0/auth0-react](https://github.com/auth0/auth0-react/tree/master/examples/gatsby-app)**
>
> master/examples/gatsby-app

---

<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:** [December 30, 2020, 11:05pm UTC](https://community.auth0.com/t/securing-nodejs-backend-as-well-as-my-react-frontend-using-auth0-lock/53647/11 "2020-12-30T23:05:02Z")

</div>

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