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, authError = true) { 21 state.authError = authError; 22 }, 23 logout() { 24 Cookies.remove('XSRF-TOKEN'); 25 } 26 }, 27 actions: { 28 login({ commit }, auth) { 29 commit('authError', false); 30 return api 31 .post('/login', { data: auth }) 32 .then(() => commit('authSuccess')) 33 .catch(error => { 34 commit('authError'); 35 throw new Error(error); 36 }); 37 }, 38 logout({ commit }) { 39 api 40 .post('/logout', { data: [] }) 41 .then(() => commit('logout')) 42 .then(() => router.go('/login')) 43 .catch(error => console.log(error)); 44 } 45 } 46}; 47 48export default AuthenticationStore; 49