localStorage & Require log in after feature not working

Hi all, I have a Vue app that I’ve got working well with Auth0 bar one bit. Since changing cacheLocation to localStorage, the Require log in after setting in the Auth0 settings page has no affect.

I want my application to automatically log users out after 480mins.

The page itself is not showing any data which is good as I’ve got refresh token expiration set to 480mins too but it’s not ideal that a user is still logged in.

What is the best way around this?

Quite surprised no one has come across this issue considering the number of websites running on Vue, React, Svelte, etc.

So the way I have solved this is by doing the following in my router file. I don’t know whether this is the most efficient method but it’s working.

const tokenClaims = await getIdTokenClaims()
const tokenExp = tokenClaims !== undefined ? moment.unix(tokenClaims?.exp).format("YYYY-MM-DD HH:mm:ss") : moment()
const timeDiff = moment(tokenExp).diff(moment(), 'seconds')

if(await isAuthenticated() && timeDiff < 0) window.location.href = `${window.location.origin}/logout`
1 Like

Hi @dally

Welcome back to the Auth0 Community!

Changing the cacheLocation to localStorage means that tokens persist even after the browser refreshes. However, while this approach provides some benefits, one downside is the token expiration handling since Auth0’s in-memory cache will respect the “Require log in after” setting. Still, when you switch to localStorage , it does not automatically handle that. Your method is a viable solution for the problem you’ve described. To fine-tune it you may want to consider background token renewal with the usage of checkSession method from the Auth0 SDK and Clear Auth0 Data as a good practice, you can do this by redirecting the user to Auth0’s logout URL.

If you found this post helpful or interesting, please give it a like :+1: Your interaction makes a difference. Have a wonderful day! :sun_with_face:

Dawid


:video_camera: Prefer how-to videos instead of written docs? We’ve got you covered! Check out our OktaDev YouTube channel for those helpful resources!

1 Like

Hi @dawid.matuszczyk, thank you for that information. I will looking into checkSession and see if it works better than my current solution.

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