1import api from "../../api"; 2import Cookies from "js-cookie"; 3 4const AuthenticationStore = { 5 namespaced: true, 6 state: { 7 status: "", 8 cookie: Cookies.get("XSRF-TOKEN") 9 }, 10 getters: { 11 authStatus: state => state.status, 12 isLoggedIn: state => !!state.cookie 13 }, 14 mutations: { 15 authRequest(state) { 16 state.status = "loading"; 17 }, 18 authSuccess(state) { 19 state.status = "authenticated"; 20 state.cookie = Cookies.get("XSRF-TOKEN"); 21 }, 22 authError(state) { 23 state.status = "error"; 24 }, 25 logout(state) { 26 state.status = ""; 27 Cookies.remove("XSRF-TOKEN"); 28 } 29 }, 30 actions: { 31 login({ commit }, auth) { 32 commit("authRequest"); 33 return api 34 .post("/login", { data: auth }) 35 .then(() => commit("authSuccess")) 36 .catch(error => { 37 commit("authError"); 38 throw new Error(error); 39 }); 40 }, 41 logout({ commit }) { 42 api 43 .post("/logout", { data: [] }) 44 .then(() => commit("logout")) 45 .catch(error => console.log(error)); 46 } 47 } 48}; 49 50export default AuthenticationStore; 51