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