# How persist the user session?

**URL:** <https://community.auth0.com/t/how-persist-the-user-session/124499>\
**Category:** Get Help\
**Tags:** classic-universal-login-experience, login-experience\
**Created:** [January 9, 2024, 7:23pm UTC](https://community.auth0.com/t/how-persist-the-user-session/124499 "2024-01-09T19:23:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lagui2003](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lagui2003/32/52262_2.png) [@lagui2003](https://community.auth0.com/u/lagui2003)\
**Post date:** [January 9, 2024, 7:23pm UTC](https://community.auth0.com/t/how-persist-the-user-session/124499/1 "2024-01-09T19:23:01Z")

</div>

Hi, I have a question how to persist the session of a user. I mean what I want is when the user uses the app the access token increase it life time. I know about the refresh token, I have enabled it but it does not work, is some other config necessary or what? Please help and if someone have a respoitory with an example, I will be thankful.

---

<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:** [January 9, 2024, 11:37pm UTC](https://community.auth0.com/t/how-persist-the-user-session/124499/3 "2024-01-09T23:37:11Z")

</div>

Hey there @lagui2003 !

> [@lagui2003](#):
>
> I mean what I want is when the user uses the app the access token increase it life time. I know about the refresh token, I have enabled it but it does not work, is some other config necessary or what?

It really depends on your particular use case, but in general refresh tokens are the way to go.  
When you say that it does not work, are you able to be a bit more specific?

Alternatively, in some cases [silent authentication](https://auth0.com/docs/authenticate/login/configure-silent-authentication) can have the same effect.

---

<div class="post-metadata">

**Author:** ![lagui2003](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lagui2003/32/52262_2.png) [@lagui2003](https://community.auth0.com/u/lagui2003)\
**Post date:** [January 10, 2024, 12:06am UTC](https://community.auth0.com/t/how-persist-the-user-session/124499/4 "2024-01-10T00:06:45Z")

</div>

Thank you for your reply. What I mean is for example, if I have an access token that lasts 1 week, if I enter the application one day before the token expires, with the refresh token I can extend the duration of the session. Is this possible and if so, is it automatic or do I have to make an additional configuration?

---

<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:** [January 10, 2024, 12:30am UTC](https://community.auth0.com/t/how-persist-the-user-session/124499/5 "2024-01-10T00:30:17Z")

</div>

No problem, happy to help where I can!

If you have an application properly configured to use refresh tokens/refresh token rotation ([SPA for example](https://auth0.com/docs/secure/tokens/refresh-tokens#for-single-page-apps)) then whichever SDK you are using should handle this automatically for you. In [auth0-react](https://github.com/auth0/auth0-react) for example, you will need to be sure and set `useRefreshTokens` to `true` in your `Auth0Provider`:

> **[Auth0ProviderOptions | @auth0/auth0-react](https://auth0.github.io/auth0-react/interfaces/Auth0ProviderOptions.html#useRefreshTokens)**
>
> Documentation for @auth0/auth0-react

---

<div class="post-metadata">

**Author:** ![lagui2003](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lagui2003/32/52262_2.png) [@lagui2003](https://community.auth0.com/u/lagui2003)\
**Post date:** [January 10, 2024, 1:08am UTC](https://community.auth0.com/t/how-persist-the-user-session/124499/6 "2024-01-10T01:08:08Z")

</div>

In my case I am using the Nextjs SDK. How do I know if I have everything configured correctly with the refresh token, do you have any resources that can guide me .

This is the code in the middleware I am using. [https://github.com/Laguilavo10/Cost-Manager/blob/main/src/middleware.ts](https://github.com/Laguilavo10/Cost-Manager/blob/main/src/middleware.ts)

---

<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:** [January 31, 2024, 1:00am UTC](https://community.auth0.com/t/how-persist-the-user-session/124499/7 "2024-01-31T01:00:39Z")

</div>

Hey @lagui2003 sorry for the delayed response!

> [@lagui2003](#):
>
> How do I know if I have everything configured correctly with the refresh token, do you have any resources that can guide me .

It should be fairly straightforward and is outline in the SDK docs here:

> <https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#getting-a-refresh-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:** [February 14, 2024, 1:01am UTC](https://community.auth0.com/t/how-persist-the-user-session/124499/8 "2024-02-14T01:01:27Z")

</div>

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