First thing first: The approach you are thinking of (reloading the entire page) just to reset your app to the initial state and redirect the user to the Login page is WRONG.
You have multiple alternatives to handle your problem (token expiry/unauthorized access to your app)
- Implement a refresh token functionality, so whenever you face situations of token expiry you refresh user token internally without the user having to worry or even know about it, or
- if the token is not refreshable (or you don't want such functionality) then just clear out user's info from wherever you are saving it (Localstorage or only in the vuex store) and redirects the user using
router.push()
back to Login page.
- expanding on @LinusBrog answer in this post you can also extract the initial data outside the original state in a function, and then use that same function for first time initialization of your state and then later, in token expiry case when you want to reset it, and then again redirecting the user to your Login route using
router.push()
method.
In both (2) and (3) you would also need to implement route guards which, in the absence of a token, will not allow the user to go inside your application, and will always redirect him back to login.
an example route guard could be:
routes: [
{
path: '/login',
name: 'Login',
beforeEnter: guardRoute,
component: Login
},
{
path: '/dashboard',
name: 'Dashboard',
component: Dashboard,
beforeEnter: guardUserRoute,
}
]
function guardAdminRoute (to, from, next) {
// get your token here either from localstorage or Vuex store
if (!auth.token) {
next({
name: 'Login',
query: { redirect: to.fullPath }
})
} else {
next()
}
}
Now, for the cancelation of all your Axios requests, Axios support cancellation of requests since v0.15. here is a nice documentation for it.
And yes, answering your edited question, as a quick ugly hack, you can use window.location.reload() function to reload your app but then again you would need to redirect the user to the Login route.