Vuex JWT Refresh token

159 Views Asked by At

I want to know how to keep user logged in all the time, so that when his token about to expire he could refresh it and would not receive 401 status when entering guarded routes and so that his data could be reflected on page whenever he refreshed it.

My assumption is that once App.vue is opened it should fetch data from server, confirming that user is logged and requesting user data, and eventually storing it in Vuex or localStorage. Hope I am on the right way to solution

fetch('http://localhost:9000/api/refresh',{
            method:'POST',
            mode:'cors',
            body:JSON.stringify({refreshToken :localStorage.getItem('refreshToken')}),
            headers:{'Content-Type':'application/json'}
        }
        ).then(res=>{
            if(res.status===200){
                return res.json()
            }
            else return
        })
        .then(({token})=>{localStorage.setItem('token',token)
        Store.dispatch('authorize',JSON.parse(localStorage.getItem('user')))
        return })
0

There are 0 best solutions below