# Cognito: Is there a way to verify JWT tokens at front-end using aws-sdk?

**URL:** <https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146>\
**Category:** Security & IAM\
**Tags:** cognito\
**Created:** [January 26, 2020, 8:38am UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146 "2020-01-26T08:38:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![hitherto\_insignia](https://awshq.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@hitherto\_insignia](https://awshq.com/u/hitherto_insignia)\
**Post date:** [January 26, 2020, 8:38am UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/1 "2020-01-26T08:38:28Z")

</div>

I’m using aws-sdk at front-end of my web application. Till now, I’ve set-up the flow to register new users, authenticate users that will get the access token, id token, and refresh token.

I’m confused about what’s next !!!

- The access and id tokens are valid for 1 hour and refresh token for 30days, and all are in JWT format.
- So, in order to check the log-in status of the user, the access token needs to be parsed to check for the expiration time. Is it possible to do this at front end?
- Consequently, if expired then using the refresh token will provide fresh access and id tokens.

Basically, I want to check the validity of the tokens and expiration time to maintain user log-in status.

---

<div class="post-metadata">

**Author:** ![interactionjackson](https://awshq.com/letter_avatar_proxy/v4/letter/i/dec6dc/32.png) [@interactionjackson](https://awshq.com/u/interactionjackson)\
**Post date:** [January 26, 2020, 8:49am UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/2 "2020-01-26T08:49:40Z")

</div>

decode the taken the expiration. you don’t want to validate if on the client because that requires the secret

---

<div class="post-metadata">

**Author:** ![TooMuchTaurine](https://awshq.com/letter_avatar_proxy/v4/letter/t/74df32/32.png) [@TooMuchTaurine](https://awshq.com/u/TooMuchTaurine)\
**Post date:** [January 26, 2020, 9:05am UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/3 "2020-01-26T09:05:00Z")

</div>

Why would you ever validate in the front end? You need to validate when the token is used in an API…

On a separate note, it’s not recommended to use refresh tokens in SPA front end apps due to risks of exposure through various OWASP attacks.

You should be using either Code flow or implicit flow without refresh tokens.

---

<div class="post-metadata">

**Author:** ![\_gareth](https://awshq.com/letter_avatar_proxy/v4/letter/_/c37758/32.png) [@\_gareth](https://awshq.com/u/_gareth)\
**Post date:** [January 26, 2020, 9:05am UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/4 "2020-01-26T09:05:30Z")

</div>

You can use an RSA signing algorithm instead of HMAC if you want to verify the token with a public key, rather than sharing the HMAC secret, but I suspect you don’t need/want to do this.

You can just decode the token client side and get the `exp` value.

---

<div class="post-metadata">

**Author:** ![namuan](https://awshq.com/letter_avatar_proxy/v4/letter/n/c57346/32.png) [@namuan](https://awshq.com/u/namuan)\
**Post date:** [January 26, 2020, 9:30am UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/5 "2020-01-26T09:30:07Z")

</div>

Do you have a backend which can make these decisions? I don’t think it should be the frontend deciding whether the user is logged-in.

---

<div class="post-metadata">

**Author:** ![Well\_Gravity](https://awshq.com/letter_avatar_proxy/v4/letter/w/858c86/32.png) [@Well\_Gravity](https://awshq.com/u/Well_Gravity)\
**Post date:** [January 26, 2020, 9:41am UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/6 "2020-01-26T09:41:15Z")

</div>

Doing that client side would pose a security risk. I would suggest not doing that.

---

<div class="post-metadata">

**Author:** ![Totalmace](https://awshq.com/letter_avatar_proxy/v4/letter/t/e36b37/32.png) [@Totalmace](https://awshq.com/u/Totalmace)\
**Post date:** [January 26, 2020, 9:41am UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/7 "2020-01-26T09:41:22Z")

</div>

The front end of any web application should not have access to the token at all.

The token should be stored in a secure http only cookie.

[https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies)

_Just query the back end to know the login status._

there are only a few moments you want to update the login status display in the ui.

- when user logs in or out
- when you receive a unauthorized response from your api

In almost any use case it does not matter that you display that your user’s session is active when technically it has has expired.

You can also send a new token when the user interacts with your api within his timeout period so that if the user is active for a period longer than the timeout period he/she will never notice that timeout period.

This reduces your ‘invalid’ login status display to situations where the user has been idle for a long time

---

<div class="post-metadata">

**Author:** ![ltorg](https://awshq.com/letter_avatar_proxy/v4/letter/l/f05b48/32.png) [@ltorg](https://awshq.com/u/ltorg)\
**Post date:** [January 26, 2020, 11:34am UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/8 "2020-01-26T11:34:57Z")

</div>

With any of the JWTs, you should be able to read the expiry timestamp in the token with an existing JWT Javascript library. You can also verify the integrity of the token using the JWKS URL for your cognito user pool. See [https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html](https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html)

I’d definitely recommend verifying the access token both on the frontend - if a token is found - and the backend when resources e.g. the user’s data, are accessed. The verification procedure is really quick and cheap so don’t worry about load/performance. By performing this verification step, you can be sure that the tokens haven’t been tampered with and you can trust the contents of the token.

I’m not entirely sure how you have your authentication flow set up to do a refresh when your access token expires but the closest thing I could find was this thread on StackOverflow: [https://stackoverflow.com/questions/37442973/cognito-user-pool-how-to-refresh-access-token-using-refresh-token](https://stackoverflow.com/questions/37442973/cognito-user-pool-how-to-refresh-access-token-using-refresh-token)

---

<div class="post-metadata">

**Author:** ![ltorg](https://awshq.com/letter_avatar_proxy/v4/letter/l/f05b48/32.png) [@ltorg](https://awshq.com/u/ltorg)\
**Post date:** [January 26, 2020, 11:40am UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/9 "2020-01-26T11:40:30Z")

</div>

> [@interactionjackson](#):
>
> decode the taken the expiration. you don’t want to validate if on the client because that requires the secret

You don’t need the secret to validate the tokens given by Cognito as they are asymmetric tokens using a public/private key pair. All you need is the JWKS URL for your cognito pool to verify the integrity of your token to make sure your tokens haven’t been tampered with and you can trust the contents of the tokens.

---

<div class="post-metadata">

**Author:** ![fuhoi](https://awshq.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@fuhoi](https://awshq.com/u/fuhoi)\
**Post date:** [January 26, 2020, 12:36pm UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/10 "2020-01-26T12:36:46Z")

</div>

See the following package:  
[https://www.npmjs.com/package/jsonwebtoken](https://www.npmjs.com/package/jsonwebtoken)

See the following method and options:  
[https://github.com/auth0/node-jsonwebtoken/blob/master/README.md#jwtdecodetoken--options](https://github.com/auth0/node-jsonwebtoken/blob/master/README.md#jwtdecodetoken--options)

---

<div class="post-metadata">

**Author:** ![LogicalHurricane](https://awshq.com/letter_avatar_proxy/v4/letter/l/94ad74/32.png) [@LogicalHurricane](https://awshq.com/u/LogicalHurricane)\
**Post date:** [January 26, 2020, 12:37pm UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/11 "2020-01-26T12:37:07Z")

</div>

> [@interactionjackson](#):
>
> decode the taken the expiration. you don’t want to validate if on the client because that requires the secret

That’s completely incorrect. You use asymmetric keys so the secret isn’t used after the token is signed. Check out [jwt.io](http://jwt.io) for libraries you can use and more info on this topic.

---

<div class="post-metadata">

**Author:** ![LogicalHurricane](https://awshq.com/letter_avatar_proxy/v4/letter/l/94ad74/32.png) [@LogicalHurricane](https://awshq.com/u/LogicalHurricane)\
**Post date:** [January 26, 2020, 12:40pm UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/12 "2020-01-26T12:40:54Z")

</div>

If you use the amplify SDK provided by Amazon then you don’t have to validate the token yourself since the SDK will do it for you and it will also refresh the token automatically.

And yes this happens on the front end (although you can do it on the front end or on the back end).

Here’s the link: [https://aws-amplify.github.io/docs/](https://aws-amplify.github.io/docs/)

---

<div class="post-metadata">

**Author:** ![LogicalHurricane](https://awshq.com/letter_avatar_proxy/v4/letter/l/94ad74/32.png) [@LogicalHurricane](https://awshq.com/u/LogicalHurricane)\
**Post date:** [January 26, 2020, 12:42pm UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/13 "2020-01-26T12:42:26Z")

</div>

> [@LogicalHurricane](#):
>
> If you use the amplify SDK provided by Amazon then you don’t have to validate the token yourself since the SDK will do it for you and it will also refresh the token automatically. And yes this happens on the front end …

Here’s a really good breakdown of how cognito works:

[![](https://awshq.com/uploads/default/original/1X/5051101bf7978e8440104eb04e97356684dbb262.jpeg "AWS re:Invent 2017: [REPEAT] Serverless Authentication and Authorization: Identity M (SRV403-R)") ](https://www.youtube.com/watch?v=VZqG7HjT2AQ)

---

<div class="post-metadata">

**Author:** ![RufusROFLpunch](https://awshq.com/letter_avatar_proxy/v4/letter/r/8491ac/32.png) [@RufusROFLpunch](https://awshq.com/u/RufusROFLpunch)\
**Post date:** [January 26, 2020, 12:47pm UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/14 "2020-01-26T12:47:18Z")

</div>

> [@TooMuchTaurine](#):
>
> Why would you ever validate in the front end? You need to validate when the token is used in an API… On a separate note, it’s not recommended to use refresh tokens in SPA front end apps due to risks of exposure …

The amplify SDK, which is the way AWS recommends that you authenticate SPAs, uses refresh tokens.

---

<div class="post-metadata">

**Author:** ![RufusROFLpunch](https://awshq.com/letter_avatar_proxy/v4/letter/r/8491ac/32.png) [@RufusROFLpunch](https://awshq.com/u/RufusROFLpunch)\
**Post date:** [January 26, 2020, 12:50pm UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/15 "2020-01-26T12:50:37Z")

</div>

The amplify Auth package makes all of this dead simple. It automatically checks token expiration and refreshes them for you. I would use that over the AWS sdk directly in javascript.

---

<div class="post-metadata">

**Author:** ![meisteronimo](https://awshq.com/letter_avatar_proxy/v4/letter/m/6a8cbe/32.png) [@meisteronimo](https://awshq.com/u/meisteronimo)\
**Post date:** [January 26, 2020, 1:27pm UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/16 "2020-01-26T13:27:21Z")

</div>

> [@TooMuchTaurine](#):
>
> Why would you ever validate in the front end? You need to validate when the token is used in an API… On a separate note, it’s not recommended to use refresh tokens in SPA front end apps due to risks of exposure …

> Code flow or implicit flow without refresh tokens

The pkec (pixie) spec has been avail for a while. It can use refresh tokens more securely. (the tokens are not visible in the browser history):

[https://www.oauth.com/oauth2-servers/pkce/](https://www.oauth.com/oauth2-servers/pkce/)

---

<div class="post-metadata">

**Author:** ![hitherto\_insignia](https://awshq.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@hitherto\_insignia](https://awshq.com/u/hitherto_insignia)\
**Post date:** [January 26, 2020, 3:25pm UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/17 "2020-01-26T15:25:41Z")

</div>

> [@RufusROFLpunch](#):
>
> The amplify Auth package makes all of this dead simple. It automatically checks token expiration and refreshes them for you. I would use that over the AWS sdk directly in javascript.

Oh, yeah? I did not know that.

Unfortunately, I cannot move to aws amplify now since most part of my code is already written using the js sdk.

---

<div class="post-metadata">

**Author:** ![franzwong](https://awshq.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@franzwong](https://awshq.com/u/franzwong)\
**Post date:** [January 26, 2020, 4:14pm UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/18 "2020-01-26T16:14:43Z")

</div>

> [@hitherto\_insignia](#):
>
> Oh, yeah? I did not know that. Unfortunately, I cannot move to aws amplify now since most part of my code is already written using the js sdk.

How did you register new user through aws-sdk on frontend side?

---

<div class="post-metadata">

**Author:** ![cr125rider](https://awshq.com/letter_avatar_proxy/v4/letter/c/e47774/32.png) [@cr125rider](https://awshq.com/u/cr125rider)\
**Post date:** [January 26, 2020, 5:13pm UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/19 "2020-01-26T17:13:47Z")

</div>

> [@hitherto\_insignia](#):
>
> Oh, yeah? I did not know that. Unfortunately, I cannot move to aws amplify now since most part of my code is already written using the js sdk.

Do not fall into the sunk cost fallacy. If this needs to run for any amount of time it’s better to redo correctly before launching.

---

<div class="post-metadata">

**Author:** ![TooMuchTaurine](https://awshq.com/letter_avatar_proxy/v4/letter/t/74df32/32.png) [@TooMuchTaurine](https://awshq.com/u/TooMuchTaurine)\
**Post date:** [January 26, 2020, 8:17pm UTC](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146/20 "2020-01-26T20:17:53Z")

</div>

> [@meisteronimo](#):
>
> The pkec (pixie) spec has been avail for a while. It can use refresh tokens more securely. (the tokens are not visible in the browser history): …

PKCE is definately an option.

However Refresh Tokens are still accessible via XSS/CRSF attacks in spa’s, most recommendations I’ve seen say avoid refresh tokens in SPA’s.

Eg  
[https://auth0.com/docs/tokens/concepts/refresh-tokens](https://auth0.com/docs/tokens/concepts/refresh-tokens)

There is really no need to use refresh tokens, is pretty easy to request a new access token without a refresh token anyway. (Assuming you are signed in to an IDP which in most cases for a spa, you are).

[Next page](https://awshq.com/t/cognito-is-there-a-way-to-verify-jwt-tokens-at-front-end-using-aws-sdk/10146.md?page=2)
