I'm expiriencing a weird behavior in a Nuxt.js project with Vue router: I'm trying to use a route guard that is placed in route.js plugin, it's registered in the nuxt.config.js plugins section and it looks like this:
app.router.beforeEach((to, from, next) => {
to.name !== "login" && app.store.getters["session/token"] === ""
? next({ name: "login" })
: next();
});
The idea is, if a user refreshes the page he loses the token that is stored in the Vuex session module, so he should be redirected to the login page.
The weird behavior is - the route guard practically works, it redirects to the /login url, but it displays the 404 page - if i refresh the page again, it correctly redirects to the /login url/page.
I also tried directly pushing to the login page app.router.push({ name: "login" }), but to no avail.
A workaround i did is adding redirect in the 404 page:
beforeCreate() {
this.$router.push({ name: "login" });
},
but i'm not happy with it...
Does anyone have any idea what's going on?