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 | 676f2fc | 2019-12-23 20:53:49 -0600 | [diff] [blame] | 16 | state.status = 'processing'; |
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 | }, |
Derick Montague | 676f2fc | 2019-12-23 20:53:49 -0600 | [diff] [blame] | 25 | authReset(state) { |
| 26 | state.status = ''; |
| 27 | }, |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 28 | logout(state) { |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 29 | state.status = ''; |
| 30 | Cookies.remove('XSRF-TOKEN'); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 31 | } |
| 32 | }, |
| 33 | actions: { |
| 34 | login({ commit }, auth) { |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 35 | commit('authRequest'); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 36 | return api |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 37 | .post('/login', { data: auth }) |
| 38 | .then(() => commit('authSuccess')) |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 39 | .catch(error => { |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 40 | commit('authError'); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 41 | throw new Error(error); |
| 42 | }); |
| 43 | }, |
| 44 | logout({ commit }) { |
Yoshie Muranaka | 6ce1a07 | 2019-12-06 14:13:59 -0800 | [diff] [blame] | 45 | api |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 46 | .post('/logout', { data: [] }) |
| 47 | .then(() => commit('logout')) |
Yoshie Muranaka | 6ce1a07 | 2019-12-06 14:13:59 -0800 | [diff] [blame] | 48 | .catch(error => console.log(error)); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | export default AuthenticationStore; |