Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 1 | import api from '../../api'; |
| 2 | import Cookies from 'js-cookie'; |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 3 | |
| 4 | const AuthenticationStore = { |
| 5 | namespaced: true, |
| 6 | state: { |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 7 | status: '', |
| 8 | cookie: Cookies.get('XSRF-TOKEN') |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 9 | }, |
| 10 | getters: { |
| 11 | authStatus: state => state.status, |
Yoshie Muranaka | 6ce1a07 | 2019-12-06 14:13:59 -0800 | [diff] [blame] | 12 | isLoggedIn: state => !!state.cookie |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 13 | }, |
| 14 | mutations: { |
| 15 | authRequest(state) { |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 16 | state.status = 'loading'; |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 17 | }, |
Yoshie Muranaka | 6ce1a07 | 2019-12-06 14:13:59 -0800 | [diff] [blame] | 18 | authSuccess(state) { |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 19 | state.status = 'authenticated'; |
| 20 | state.cookie = Cookies.get('XSRF-TOKEN'); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 21 | }, |
| 22 | authError(state) { |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 23 | state.status = 'error'; |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 24 | }, |
| 25 | logout(state) { |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 26 | state.status = ''; |
| 27 | Cookies.remove('XSRF-TOKEN'); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 28 | } |
| 29 | }, |
| 30 | actions: { |
| 31 | login({ commit }, auth) { |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 32 | commit('authRequest'); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 33 | return api |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 34 | .post('/login', { data: auth }) |
| 35 | .then(() => commit('authSuccess')) |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 36 | .catch(error => { |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 37 | commit('authError'); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 38 | throw new Error(error); |
| 39 | }); |
| 40 | }, |
| 41 | logout({ commit }) { |
Yoshie Muranaka | 6ce1a07 | 2019-12-06 14:13:59 -0800 | [diff] [blame] | 42 | api |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 43 | .post('/logout', { data: [] }) |
| 44 | .then(() => commit('logout')) |
Yoshie Muranaka | 6ce1a07 | 2019-12-06 14:13:59 -0800 | [diff] [blame] | 45 | .catch(error => console.log(error)); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | export default AuthenticationStore; |