SurenNeware | 6185909 | 2020-10-01 09:37:32 +0530 | [diff] [blame^] | 1 | import api from '@/store/api'; |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 2 | import Cookies from 'js-cookie'; |
SurenNeware | 6185909 | 2020-10-01 09:37:32 +0530 | [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 | 23f227d | 2020-05-01 14:19:43 -0700 | [diff] [blame] | 13 | isLoggedIn: state => !!state.cookie, |
| 14 | token: state => state.cookie |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 15 | }, |
| 16 | mutations: { |
Yoshie Muranaka | 6ce1a07 | 2019-12-06 14:13:59 -0800 | [diff] [blame] | 17 | authSuccess(state) { |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 18 | state.authError = false; |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 19 | state.cookie = Cookies.get('XSRF-TOKEN'); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 20 | }, |
Derick Montague | a06fe46 | 2020-03-11 13:48:42 -0500 | [diff] [blame] | 21 | authError(state, authError = true) { |
| 22 | state.authError = authError; |
Derick Montague | 676f2fc | 2019-12-23 20:53:49 -0600 | [diff] [blame] | 23 | }, |
Derick Montague | c031b69 | 2020-02-12 15:55:42 -0600 | [diff] [blame] | 24 | logout() { |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 25 | Cookies.remove('XSRF-TOKEN'); |
Sukanya Pandey | b1f559f | 2020-04-28 20:18:28 +0530 | [diff] [blame] | 26 | localStorage.removeItem('storedUsername'); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 27 | } |
| 28 | }, |
| 29 | actions: { |
| 30 | login({ commit }, auth) { |
Derick Montague | a06fe46 | 2020-03-11 13:48:42 -0500 | [diff] [blame] | 31 | commit('authError', false); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 32 | return api |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 33 | .post('/login', { data: auth }) |
| 34 | .then(() => commit('authSuccess')) |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 35 | .catch(error => { |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 36 | commit('authError'); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 37 | throw new Error(error); |
| 38 | }); |
| 39 | }, |
| 40 | logout({ commit }) { |
Yoshie Muranaka | 6ce1a07 | 2019-12-06 14:13:59 -0800 | [diff] [blame] | 41 | api |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 42 | .post('/logout', { data: [] }) |
| 43 | .then(() => commit('logout')) |
Derick Montague | c031b69 | 2020-02-12 15:55:42 -0600 | [diff] [blame] | 44 | .then(() => router.go('/login')) |
Yoshie Muranaka | 6ce1a07 | 2019-12-06 14:13:59 -0800 | [diff] [blame] | 45 | .catch(error => console.log(error)); |
Yoshie Muranaka | 2c98b09 | 2020-06-22 13:28:09 -0700 | [diff] [blame] | 46 | }, |
| 47 | async checkPasswordChangeRequired(_, username) { |
| 48 | return await api |
| 49 | .get(`/redfish/v1/AccountService/Accounts/${username}`) |
| 50 | .then(({ data: { PasswordChangeRequired } }) => PasswordChangeRequired) |
| 51 | .catch(error => console.log(error)); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | export default AuthenticationStore; |