# Session not persisting using cacheLocation: localstorage

**URL:** <https://community.auth0.com/t/session-not-persisting-using-cachelocation-localstorage/55364>\
**Category:** Get Help\
**Tags:** sessions, localstorage, jwt, token\
**Created:** [December 26, 2020, 7:22pm UTC](https://community.auth0.com/t/session-not-persisting-using-cachelocation-localstorage/55364 "2020-12-26T19:22:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![garym](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/garym/32/18281_2.png) [@garym](https://community.auth0.com/u/garym)\
**Post date:** [December 26, 2020, 7:22pm UTC](https://community.auth0.com/t/session-not-persisting-using-cachelocation-localstorage/55364/1 "2020-12-26T19:22:07Z")

</div>

I have authentication with claims working except _sessions are not persisting_ when refreshing the browser. I’m using **auth0-spa-js** with vanlilla JS (well, Svelte actually), I have `cacheLocation: "localstorage"` when calling `createAuth0Client`, I see the @@auth0spajs@@ entries in localstorage, yet when refreshing my browser window, it never persists the session.

Could this be an issue with me using `token = auth0.getIdTokenClaims. __raw` to get the RAW token to include in my headers instead of using `token = auth0.getTokenSilently` which I think is called automatically when refreshing. Retrieving the `__ raw` token this way works: on the Dgraph end I’m able to get the custom claims injected by an **Auth0 rule** I created..

If I use `auth0.getTokenSilently`, I get an opaque access\_token because I’m missing the `audience` property. The audience that dgraph recommends to put in dgraph schema is AUTH0-APP-CLIENT-ID (e.g. “FstBEWxxxxxxxxxxxxxxxxxxxxxx\_wqp”), yet when I try to add that as the `audience` value when calling `createAuth0Client` I get the error “Service not found” for that audience value. The ONLY way I don’t get the service not found is if I use `audience: "https://my-app.auth0.com/api/v2/"` at which point, it works – `auth0.getTokenSilently` returns a real JWT… BUT WITH NO USER INFO NOR MY CLAIMS (I confirm it at [jwt.io](http://jwt.io)).

FYI - I’m have google oauth social as an identity provider.

So… What should I do to get sessions persisting?

- Stick to using `auth0.getIdTokenClaims.__raw` for headers AND try to figure out how to fix the refresh? Any ideas on how to get session working in that case?

- Or should I try to use `auth0.getTokenSilently` to get a JWT by using audience of `https://my-app.auth0.com/api/v2/` AND figure out how to get the user info and claims injected by my Auth0 Rule which don’t appear in it? Any ideas on how to get that? And would this address the original issue of session not retaining on browser refresh?

Thank you for any help!

---

<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 30, 2020, 10:48pm UTC](https://community.auth0.com/t/session-not-persisting-using-cachelocation-localstorage/55364/3 "2020-12-30T22:48:36Z")

</div>

Hi @garym,

Welcome back.

Are you using [refresh token rotation](https://auth0.com/docs/libraries/auth0-single-page-app-sdk#use-rotating-refresh-tokens)? That might help solve some of the persistence issues.

As for the opaque token issues, it looks like the audience you are requesting the token for is not registered in your dashboard. The audience param is meant to indicate which API is going to consume the token. It shouldn’t simply be a client id. It needs to be a registered API with an _identifier_, that is why the management API URI is working for you.

As for the claims being returned…this could be because you aren’t requesting enough scopes. I would try and figure out the audience issue and see if you are still encountering this problem.

---

<div class="post-metadata">

**Author:** ![garym](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/garym/32/18281_2.png) [@garym](https://community.auth0.com/u/garym)\
**Post date:** [December 31, 2020, 7:21pm UTC](https://community.auth0.com/t/session-not-persisting-using-cachelocation-localstorage/55364/4 "2020-12-31T19:21:58Z")

</div>

Hi Dan,

Thanks for responding. Yes, I have useRefreshTokens to true.

The claims work fine with me getting the \_\_raw token, so unless there’s a problem with doing that, I’ll just keep using that for now.

The main issue is SESSIONS are not persisting. Is there something wrong I’m doing that refreshing the browser doesn’t retain the session? I see the localstate storage… somehow it’s not persisting. Thanks for any help.

---

<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 31, 2020, 9:07pm UTC](https://community.auth0.com/t/session-not-persisting-using-cachelocation-localstorage/55364/5 "2020-12-31T21:07:34Z")

</div>

Can you DM me a HAR of the login?

> **[Generate and Analyze HAR Files](https://auth0.com/docs/troubleshoot/troubleshooting-tools/generate-and-analyze-har-files)**
>
> Learn how to troubleshoot with HAR files and steps to generate a HAR file.

---

<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:** [January 21, 2021, 7:03pm UTC](https://community.auth0.com/t/session-not-persisting-using-cachelocation-localstorage/55364/6 "2021-01-21T19:03:19Z")

</div>

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