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