1import api from '../../api';
2import Cookies from 'js-cookie';
3
4const AuthenticationStore = {
5  namespaced: true,
6  state: {
7    authError: false,
8    cookie: Cookies.get('XSRF-TOKEN')
9  },
10  getters: {
11    authError: state => state.authError,
12    isLoggedIn: state => !!state.cookie
13  },
14  mutations: {
15    authSuccess(state) {
16      state.authError = false;
17      state.cookie = Cookies.get('XSRF-TOKEN');
18    },
19    authError(state) {
20      state.authError = true;
21    },
22    logout(state) {
23      state.authError = false;
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        .catch(error => console.log(error));
42    }
43  }
44};
45
46export default AuthenticationStore;
47