-1

am beginner with vuejs. I am doing my first application with Vuejs with laravel sanctum as a package for token based authentication. My problem is when a user is logged in and stay inactive for several hours without signout from application , i got 401 (unauthorized) error after trying to access protected routes.. I must signout and signin again in order to do my jobs. I am using vuex state management library to preseve the app state.
routes/api.php

Route::post('/login','API\AuthController@login');
 Route::middleware('auth:sanctum')->group(function (){  
       Route::get('/logout','API\AuthController@logout');});

Below is how i am logging in

axios.get('/sanctum/csrf-cookie').then(response => {
                // Login...
                this.$store.dispatch('login',credentials)
                   .then((res) => {
                       this.$store.commit("SET_LOADING",false);
                       if( this.isAuthenticated && config.getToken() !== 'undefined'){
                            this.$router.push({ name: 'home'});
                            window.reload;
                       }
                   });});

My vuex store.js

export default new Vuex.Store({
modules: {
    auth
},
plugins: [
    createPersistedState(
        {
            storage: window.localStorage
        }
    )],})

i tried to store my tokens at local storage but i don't think its a best way... What i want is to make a user logged in for much long untill he/she logged out

SAID KHAMIS
  • 67
  • 2
  • 11

1 Answers1

0

Thank you, i got myself an answer. Based on the following statement from Laravel Sanctum Official documentation

Of course, if your user's session expires due to lack of activity, subsequent requests to the Laravel application may receive 401 or 419 HTTP error response. In this case, you should redirect the user to your SPA's login page.

I will redirect user to login form after receiving 401 0r 419 HTTP response

SAID KHAMIS
  • 67
  • 2
  • 11