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