i am using vuex for statemanagement this is my vuex code ,
here my logic is if token is exist in local storage then it will set isAuthenticated to true so it will be easy to check against routes.but some how its not working.
import axios from "axios";
import router from "../../router";
const state = {
token: localStorage.getItem("tkn") || "",
isAuth: false,
};
const getters = {
isAuthenticated: (state) => !!state.token,
authStatus: (state) => state.isAuth,
};
const actions = {
login({ commit }, loginData) {
// commit('auth_request')
axios
.post("/api/adminuser/auth/login", loginData)
.then((resp) => {
console.log(resp);
localStorage.setItem("tkn", resp.data.token);
axios.defaults.headers.common["Authorization"] = resp.data.token;
commit("setToken", resp.data.token);
router.push("/");
})
.catch((err) => {
console.log(err);
commit("authError", err);
localStorage.removeItem("token");
});
},
};
const mutations = {
setToken: (state, token) => ((state.token = token), (state.isAuth = true)),
authError: (state) => (state.isAuth = false),
};
export default {
state,
getters,
actions,
mutations,
};
and this is my router code
import Vue from "vue";
import VueRouter from "vue-router";
import auth from "../store/modules/auth.js";
Vue.use(VueRouter);
const routes = [
{
path: "/signin",
name: "signin",
component: () => import("../components/Authentication/Signin.vue"),
},
{
path: "/",
name: "dashboard",
component: () => import("../components/Dashboard/Dashboard.vue"),
meta: {
requiresAuth: true,
},
},
];
const router = new VueRouter({
mode: "history",
routes,
});
router.beforeEach((to, from, next) => {
if (to.name !== "signin" && auth.getters.isAuthenticated == false)
next({ name: "signin" });
else next();
});
export default router;
currently if i do login still i can go to signin page and even i can goto dashboard without authentication.