blob: 3a554b6b78d5a0933116b597e54769d72e95c32e [file] [log] [blame]
Derick Montaguefded0d12019-12-11 06:16:40 -06001import api from '../../api';
2import Cookies from 'js-cookie';
Derick Montaguee080a1a2019-12-04 16:30:08 -06003
4const AuthenticationStore = {
5 namespaced: true,
6 state: {
Derick Montaguefded0d12019-12-11 06:16:40 -06007 status: '',
8 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) {
Derick Montague676f2fc2019-12-23 20:53:49 -060016 state.status = 'processing';
Derick Montaguee080a1a2019-12-04 16:30:08 -060017 },
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080018 authSuccess(state) {
Derick Montaguefded0d12019-12-11 06:16:40 -060019 state.status = 'authenticated';
20 state.cookie = Cookies.get('XSRF-TOKEN');
Derick Montaguee080a1a2019-12-04 16:30:08 -060021 },
22 authError(state) {
Derick Montaguefded0d12019-12-11 06:16:40 -060023 state.status = 'error';
Derick Montaguee080a1a2019-12-04 16:30:08 -060024 },
Derick Montague676f2fc2019-12-23 20:53:49 -060025 authReset(state) {
26 state.status = '';
27 },
Derick Montaguee080a1a2019-12-04 16:30:08 -060028 logout(state) {
Derick Montaguefded0d12019-12-11 06:16:40 -060029 state.status = '';
30 Cookies.remove('XSRF-TOKEN');
Derick Montaguee080a1a2019-12-04 16:30:08 -060031 }
32 },
33 actions: {
34 login({ commit }, auth) {
Derick Montaguefded0d12019-12-11 06:16:40 -060035 commit('authRequest');
Derick Montaguee080a1a2019-12-04 16:30:08 -060036 return api
Derick Montaguefded0d12019-12-11 06:16:40 -060037 .post('/login', { data: auth })
38 .then(() => commit('authSuccess'))
Derick Montaguee080a1a2019-12-04 16:30:08 -060039 .catch(error => {
Derick Montaguefded0d12019-12-11 06:16:40 -060040 commit('authError');
Derick Montaguee080a1a2019-12-04 16:30:08 -060041 throw new Error(error);
42 });
43 },
44 logout({ commit }) {
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080045 api
Derick Montaguefded0d12019-12-11 06:16:40 -060046 .post('/logout', { data: [] })
47 .then(() => commit('logout'))
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080048 .catch(error => console.log(error));
Derick Montaguee080a1a2019-12-04 16:30:08 -060049 }
50 }
51};
52
53export default AuthenticationStore;