# LOCK and SPA, how does the server know if the access\_token is valid?

**URL:** <https://community.auth0.com/t/lock-and-spa-how-does-the-server-know-if-the-access-token-is-valid/6819>\
**Category:** Get Help\
**Tags:** lock\
**Created:** [May 31, 2017, 11:42pm UTC](https://community.auth0.com/t/lock-and-spa-how-does-the-server-know-if-the-access-token-is-valid/6819 "2017-05-31T23:42:12Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelwjoyner](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@michaelwjoyner](https://community.auth0.com/u/michaelwjoyner)\
**Post date:** [May 31, 2017, 11:42pm UTC](https://community.auth0.com/t/lock-and-spa-how-does-the-server-know-if-the-access-token-is-valid/6819/1 "2017-05-31T23:42:12Z")

</div>

If a SPA auth’s with Auth0 and get’s it access\_token, how can the server know if the client has a valid token or maybe their context/user data?

---

<div class="post-metadata">

**Author:** ![prashantT](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/prashantt/32/6002_2.png) [@prashantT](https://community.auth0.com/u/prashantT)\
**Post date:** [June 1, 2017, 6:22am UTC](https://community.auth0.com/t/lock-and-spa-how-does-the-server-know-if-the-access-token-is-valid/6819/2 "2017-06-01T06:22:01Z")

</div>

When an access\_token is passed to the server as a Bearer token, the server should perform the necessary validation. In short, the following checks should be performed:

- Check that the JWT is well formed
- Check the signature
- Validate the standard claims
- Check the client permissions (scopes)

These checks can be done using any of the relevant SDK’s - here is a sample implementation for a Node.js API:

> **[Server Apps + API: Node.js Implementation for the API](https://auth0.com/docs/get-started/architecture-scenarios/server-application-api/api-implementation-nodejs)**
>
> The Node.js implementation of the API for the Server Client + API architecture scenario

The following document provides more information on verifying access tokens:

> **[Validate Access Tokens](https://auth0.com/docs/secure/tokens/access-tokens/validate-access-tokens)**
>
> Describes how to validate an access token.

---

<div class="post-metadata">

**Author:** ![michaelwjoyner](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@michaelwjoyner](https://community.auth0.com/u/michaelwjoyner)\
**Post date:** [June 1, 2017, 6:30am UTC](https://community.auth0.com/t/lock-and-spa-how-does-the-server-know-if-the-access-token-is-valid/6819/3 "2017-06-01T06:30:12Z")

</div>

@prashant

I think I lost you by my explanation.

1. Single Page Application uses LOCK to sign in and gets access\_token.
2. SPA makes API request to server, sends access\_token.

How does the API server know if access\_token from client is valid?

---

<div class="post-metadata">

**Author:** ![michaelwjoyner](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@michaelwjoyner](https://community.auth0.com/u/michaelwjoyner)\
**Post date:** [June 1, 2017, 7:03am UTC](https://community.auth0.com/t/lock-and-spa-how-does-the-server-know-if-the-access-token-is-valid/6819/4 "2017-06-01T07:03:47Z")

</div>

@prashant

I think I see, so the SPA will forward the access\_token to the server and then the server will do the validation on the jwt token.

Thank you

---

<div class="post-metadata">

**Author:** ![michaelwjoyner](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@michaelwjoyner](https://community.auth0.com/u/michaelwjoyner)\
**Post date:** [June 1, 2017, 7:14am UTC](https://community.auth0.com/t/lock-and-spa-how-does-the-server-know-if-the-access-token-is-valid/6819/5 "2017-06-01T07:14:12Z")

</div>

@prashant

Can I please ask some more questions, I appreciate your help.

I understand now that the SPA will get a JWT token from LOCK. Then then it will pass it as a Header “Authorization : Bearer xx.xx.xx” to API server.

So if I am using HMACSHA256 in the signature, is the secret setup in Auth0 control panel and Auth0 will use that secret to make the JWT that LOCK will return to the SPA?

I hope that made sense. In other words if Auth0 and the API server knew the secret on JWT that makes total sense.

---

<div class="post-metadata">

**Author:** ![michaelwjoyner](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@michaelwjoyner](https://community.auth0.com/u/michaelwjoyner)\
**Post date:** [June 1, 2017, 7:22am UTC](https://community.auth0.com/t/lock-and-spa-how-does-the-server-know-if-the-access-token-is-valid/6819/6 "2017-06-01T07:22:49Z")

</div>

@prashant  
Will all the app\_data, and user\_data be in the payload?

---

<div class="post-metadata">

**Author:** ![prashantT](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/prashantt/32/6002_2.png) [@prashantT](https://community.auth0.com/u/prashantT)\
**Post date:** [June 1, 2017, 9:34am UTC](https://community.auth0.com/t/lock-and-spa-how-does-the-server-know-if-the-access-token-is-valid/6819/7 "2017-06-01T09:34:41Z")

</div>

This is a separate question. Please read through the following docs on how to add app\_metadata or user\_metadata attributes to the token claims: [OpenID Connect Scopes](https://auth0.com/docs/api-auth/tutorials/adoption/scope-custom-claims)

---

<div class="post-metadata">

**Author:** ![michaelwjoyner](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@michaelwjoyner](https://community.auth0.com/u/michaelwjoyner)\
**Post date:** [June 1, 2017, 6:30am UTC](https://community.auth0.com/t/lock-and-spa-how-does-the-server-know-if-the-access-token-is-valid/6819/8 "2017-06-01T06:30:12Z")

</div>

@prashant

I think I lost you by my explanation.

1. Single Page Application uses LOCK to sign in and gets access\_token.
2. SPA makes API request to server, sends access\_token.

How does the API server know if access\_token from client is valid?

---

<div class="post-metadata">

**Author:** ![prashantT](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/prashantt/32/6002_2.png) [@prashantT](https://community.auth0.com/u/prashantT)\
**Post date:** [June 1, 2017, 6:56am UTC](https://community.auth0.com/t/lock-and-spa-how-does-the-server-know-if-the-access-token-is-valid/6819/9 "2017-06-01T06:56:11Z")

</div>

@michaelwjoyner Please read the document I mention in my answer. That provides information on how the server knows if the access\_token is valid. As mentioned, if you use one of our SDK’s, the SDK will do the necessary checks to validate the token, including checking the expiration, etc.

---

<div class="post-metadata">

**Author:** ![michaelwjoyner](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@michaelwjoyner](https://community.auth0.com/u/michaelwjoyner)\
**Post date:** [June 1, 2017, 7:03am UTC](https://community.auth0.com/t/lock-and-spa-how-does-the-server-know-if-the-access-token-is-valid/6819/10 "2017-06-01T07:03:47Z")

</div>

@prashant

I think I see, so the SPA will forward the access\_token to the server and then the server will do the validation on the jwt token.

Thank you

---

<div class="post-metadata">

**Author:** ![prashantT](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/prashantt/32/6002_2.png) [@prashantT](https://community.auth0.com/u/prashantT)\
**Post date:** [June 1, 2017, 7:07am UTC](https://community.auth0.com/t/lock-and-spa-how-does-the-server-know-if-the-access-token-is-valid/6819/11 "2017-06-01T07:07:28Z")

</div>

Correct, the SPA will pass the access\_token in the Authorization header when making a call to the API - the API will then validate this token.

---

<div class="post-metadata">

**Author:** ![michaelwjoyner](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@michaelwjoyner](https://community.auth0.com/u/michaelwjoyner)\
**Post date:** [June 1, 2017, 7:14am UTC](https://community.auth0.com/t/lock-and-spa-how-does-the-server-know-if-the-access-token-is-valid/6819/12 "2017-06-01T07:14:12Z")

</div>

@prashant

Can I please ask some more questions, I appreciate your help.

I understand now that the SPA will get a JWT token from LOCK. Then then it will pass it as a Header “Authorization : Bearer xx.xx.xx” to API server.

So if I am using HMACSHA256 in the signature, is the secret setup in Auth0 control panel and Auth0 will use that secret to make the JWT that LOCK will return to the SPA?

I hope that made sense. In other words if Auth0 and the API server knew the secret on JWT that makes total sense.

---

<div class="post-metadata">

**Author:** ![prashantT](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/prashantt/32/6002_2.png) [@prashantT](https://community.auth0.com/u/prashantT)\
**Post date:** [June 1, 2017, 7:20am UTC](https://community.auth0.com/t/lock-and-spa-how-does-the-server-know-if-the-access-token-is-valid/6819/13 "2017-06-01T07:20:22Z")

</div>

Correct. **Note, that our recommended approach is to use RS256** , to minimize the risk of leaking the client secret. If using RS256, the API will validate the token using the public key obtained from `https://{YOUR_AUTH0_DOMAIN}/.well-known/jwks.json`. This is outlined in the sample:

> **[Server Apps + API: Node.js Implementation for the API](https://auth0.com/docs/get-started/architecture-scenarios/server-application-api/api-implementation-nodejs)**
>
> The Node.js implementation of the API for the Server Client + API architecture scenario

If you choose to use HS256 (not recommended), the API will need to be configured with the client secret, which will be used to validate the signature of the token.

---

<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 2, 2018, 5:54am UTC](https://community.auth0.com/t/lock-and-spa-how-does-the-server-know-if-the-access-token-is-valid/6819/14 "2018-03-02T05:54:14Z")

</div>


