How to clear local storage when session cookie expires in laravel and vue app

Viewed 25

I have a Vue app within a Laravel app. On logging in a user is issued authenticated session cookie by Laravel and after that, I set authenticated to true in the local storage localStorage.setItem("authenticated", "true"). When the session expires authenticated is still true. How can I remove the key when the session expires?

1 Answers

Check session()->get("session_name") in your controller.

You can use this code below, just add in your created() or mounted() option

Check if session expired or not from API and change your localStorage key value;

<script>
export default {
 mounted() {
  fetch('http://yourserver.com/checkSession').then(async response => {
  const status = await response.json();
if(!status.session) {
localStorage.setItem("authenticated", "false")
}
})
 }
}
</script>
Related