# How to get refresh tokens to work in Single Page Application (SPA) API

**URL:** <https://community.auth0.com/t/how-to-get-refresh-tokens-to-work-in-single-page-application-spa-api/128888>\
**Category:** Get Help\
**Tags:** apis, application\
**Created:** [March 4, 2024, 9:33pm UTC](https://community.auth0.com/t/how-to-get-refresh-tokens-to-work-in-single-page-application-spa-api/128888 "2024-03-04T21:33:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbarnell](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/bbarnell/32/50695_2.png) [@bbarnell](https://community.auth0.com/u/bbarnell)\
**Post date:** [March 4, 2024, 9:33pm UTC](https://community.auth0.com/t/how-to-get-refresh-tokens-to-work-in-single-page-application-spa-api/128888/1 "2024-03-04T21:33:07Z")

</div>

I am trying to follow this [topic](https://community.auth0.com/t/dashboard-settings-to-disable-third-party-cookies/127247), but don’t fully understand how to make it work. Here is my code:

createAuth0Client({  
domain: Settings.auth0\_domain,  
clientId: Settings.auth0\_clientId,  
useRefreshTokens: true,  
authorizationParams: { audience: “offline\_access”, scope: “offline\_access”, prompt: “none” },

}).then(auth0Client =\> {

@ty.frith - Can you help me with this.

---

<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:** [March 5, 2024, 12:01am UTC](https://community.auth0.com/t/how-to-get-refresh-tokens-to-work-in-single-page-application-spa-api/128888/3 "2024-03-05T00:01:59Z")

</div>

Hey there @bbarnell !

At a most basic level, [all that’s required](https://github.com/auth0/auth0-spa-js/blob/main/EXAMPLES.md#refresh-tokens) is setting `useRefreshTokens: true` and enabling refresh token rotation for the relevant application in your dashboard settings.

Here’s a [working example](https://github.com/tyfrth/auth0-javascript-samples/blob/fa8a93bdce1c5e287350d2d05b177e78be38dd53/02-Calling-an-API/public/js/app.js#L58) if you’d like to check it out - If I omit the `useRefreshTokens: true` flag then the application will resort to using [silent auth](https://auth0.com/docs/authenticate/login/configure-silent-authentication) if I click the “ping” button ([getTokenSilently](https://github.com/tyfrth/auth0-javascript-samples/blob/fa8a93bdce1c5e287350d2d05b177e78be38dd53/02-Calling-an-API/public/js/app.js#L85)) of the external API page. With `useRefreshTokens: true`, you can see the `offline_access` scope included in the initial authorize request constructed on login, and subsequently a refresh token returned from the token endpoint:

 ![Screenshot 2024-03-04 at 3.49.07 PM](https://global.discourse-cdn.com/auth0/original/3X/a/a/aa45f8d67a069ded37902a8b228e60315d9a4188.png)

 ![Screenshot 2024-03-04 at 3.49.54 PM](https://global.discourse-cdn.com/auth0/original/3X/9/d/9d464add0ad8c0e86525d82ec93eed129b443b9a.png)

Now when I click “ping api”, I can see a successful refresh token exchange in my logs (monitoring → logs):

 ![Screenshot 2024-03-04 at 3.59.28 PM](https://global.discourse-cdn.com/auth0/original/3X/0/f/0fb74f036c78a805a79e62525ae8a0e98eb93a0d.jpeg)

---

<div class="post-metadata">

**Author:** ![Jerome.Piquet.AD](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jerome.piquet.ad/32/36960_2.png) [@Jerome.Piquet.AD](https://community.auth0.com/u/Jerome.Piquet.AD)\
**Post date:** [March 11, 2024, 5:32pm UTC](https://community.auth0.com/t/how-to-get-refresh-tokens-to-work-in-single-page-application-spa-api/128888/5 "2024-03-11T17:32:37Z")

</div>

Hello,

With a SPA, in order to get the refresh token from the /token endpoint:

- SPA: set config **useRefreshTokens** to true
- API Config: set **Allow Offline Access** to true
- Application config: Advanced Settings \ Grant Types \ **Refresh Token** enable
- Application config: enable **Refresh Token Rotation**

Enabling RTR was the key for me to retrieve the refresh token.  
Hope this helps!

---

<div class="post-metadata">

**Author:** ![bbarnell](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/bbarnell/32/50695_2.png) [@bbarnell](https://community.auth0.com/u/bbarnell)\
**Post date:** [March 11, 2024, 7:10pm UTC](https://community.auth0.com/t/how-to-get-refresh-tokens-to-work-in-single-page-application-spa-api/128888/6 "2024-03-11T19:10:35Z")

</div>

@Jerome.Piquet.AD I have most if it working, except when I logout in the application without leaving the application, and then log back in again, the application users 3rd party cookies. I don’t understand why Auth0 did not use cookies on the first login, but it uses them the second time?

---

<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 25, 2024, 7:10pm UTC](https://community.auth0.com/t/how-to-get-refresh-tokens-to-work-in-single-page-application-spa-api/128888/7 "2024-03-25T19:10:55Z")

</div>

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