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