blob: 3512e2da272a2a7b94e3bc6a5ed3a19195aec22b [file] [log] [blame]
Derick Montaguee080a1a2019-12-04 16:30:08 -06001import api from "../../api";
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -08002import Cookies from "js-cookie";
Derick Montaguee080a1a2019-12-04 16:30:08 -06003
4const AuthenticationStore = {
5 namespaced: true,
6 state: {
Derick Montaguee080a1a2019-12-04 16:30:08 -06007 status: "",
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -08008 cookie: Cookies.get("XSRF-TOKEN")
Derick Montaguee080a1a2019-12-04 16:30:08 -06009 },
10 getters: {
11 authStatus: state => state.status,
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080012 isLoggedIn: state => !!state.cookie
Derick Montaguee080a1a2019-12-04 16:30:08 -060013 },
14 mutations: {
15 authRequest(state) {
16 state.status = "loading";
17 },
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080018 authSuccess(state) {
19 state.status = "authenticated";
20 state.cookie = Cookies.get("XSRF-TOKEN");
Derick Montaguee080a1a2019-12-04 16:30:08 -060021 },
22 authError(state) {
23 state.status = "error";
24 },
25 logout(state) {
26 state.status = "";
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080027 Cookies.remove("XSRF-TOKEN");
Derick Montaguee080a1a2019-12-04 16:30:08 -060028 }
29 },
30 actions: {
31 login({ commit }, auth) {
32 commit("authRequest");
33 return api
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080034 .post("/login", { data: auth })
35 .then(() => commit("authSuccess"))
Derick Montaguee080a1a2019-12-04 16:30:08 -060036 .catch(error => {
37 commit("authError");
Derick Montaguee080a1a2019-12-04 16:30:08 -060038 throw new Error(error);
39 });
40 },
41 logout({ commit }) {
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080042 api
43 .post("/logout", { data: [] })
44 .then(() => commit("logout"))
45 .catch(error => console.log(error));
Derick Montaguee080a1a2019-12-04 16:30:08 -060046 }
47 }
48};
49
50export default AuthenticationStore;