summaryrefslogtreecommitdiff
path: root/src/store/modules/Authentication/AuthenticanStore.js
blob: 3512e2da272a2a7b94e3bc6a5ed3a19195aec22b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import api from "../../api";
import Cookies from "js-cookie";

const AuthenticationStore = {
  namespaced: true,
  state: {
    status: "",
    cookie: Cookies.get("XSRF-TOKEN")
  },
  getters: {
    authStatus: state => state.status,
    isLoggedIn: state => !!state.cookie
  },
  mutations: {
    authRequest(state) {
      state.status = "loading";
    },
    authSuccess(state) {
      state.status = "authenticated";
      state.cookie = Cookies.get("XSRF-TOKEN");
    },
    authError(state) {
      state.status = "error";
    },
    logout(state) {
      state.status = "";
      Cookies.remove("XSRF-TOKEN");
    }
  },
  actions: {
    login({ commit }, auth) {
      commit("authRequest");
      return api
        .post("/login", { data: auth })
        .then(() => commit("authSuccess"))
        .catch(error => {
          commit("authError");
          throw new Error(error);
        });
    },
    logout({ commit }) {
      api
        .post("/logout", { data: [] })
        .then(() => commit("logout"))
        .catch(error => console.log(error));
    }
  }
};

export default AuthenticationStore;