blob: 0dca1832b9be6b21a27bbb9c00c886b13b0e4494 [file] [log] [blame]
SurenNeware61859092020-10-01 09:37:32 +05301import api from '@/store/api';
Derick Montaguefded0d12019-12-11 06:16:40 -06002import Cookies from 'js-cookie';
SurenNeware61859092020-10-01 09:37:32 +05303import router from '@/router';
Derick Montaguee080a1a2019-12-04 16:30:08 -06004
5const AuthenticationStore = {
6 namespaced: true,
7 state: {
kirankumarb07b89eed22023-01-12 15:50:30 +05308 consoleWindow: null,
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -08009 authError: false,
Yoshie Muranakad624dae2020-09-21 14:35:58 -070010 xsrfCookie: Cookies.get('XSRF-TOKEN'),
Derick Montague602e98a2020-10-21 16:20:00 -050011 isAuthenticatedCookie: Cookies.get('IsAuthenticated'),
Derick Montaguee080a1a2019-12-04 16:30:08 -060012 },
13 getters: {
kirankumarb07b89eed22023-01-12 15:50:30 +053014 consoleWindow: (state) => state.consoleWindow,
Derick Montague602e98a2020-10-21 16:20:00 -050015 authError: (state) => state.authError,
16 isLoggedIn: (state) => {
Yoshie Muranakad624dae2020-09-21 14:35:58 -070017 return (
18 state.xsrfCookie !== undefined || state.isAuthenticatedCookie == 'true'
19 );
20 },
Derick Montague602e98a2020-10-21 16:20:00 -050021 token: (state) => state.xsrfCookie,
Derick Montaguee080a1a2019-12-04 16:30:08 -060022 },
23 mutations: {
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080024 authSuccess(state) {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080025 state.authError = false;
Yoshie Muranakad624dae2020-09-21 14:35:58 -070026 state.xsrfCookie = Cookies.get('XSRF-TOKEN');
Derick Montaguee080a1a2019-12-04 16:30:08 -060027 },
Derick Montaguea06fe462020-03-11 13:48:42 -050028 authError(state, authError = true) {
29 state.authError = authError;
Derick Montague676f2fc2019-12-23 20:53:49 -060030 },
Yoshie Muranakad624dae2020-09-21 14:35:58 -070031 logout(state) {
Derick Montaguefded0d12019-12-11 06:16:40 -060032 Cookies.remove('XSRF-TOKEN');
Yoshie Muranakad624dae2020-09-21 14:35:58 -070033 Cookies.remove('IsAuthenticated');
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053034 localStorage.removeItem('storedUsername');
Yoshie Muranakad624dae2020-09-21 14:35:58 -070035 state.xsrfCookie = undefined;
36 state.isAuthenticatedCookie = undefined;
Derick Montague602e98a2020-10-21 16:20:00 -050037 },
kirankumarb07b89eed22023-01-12 15:50:30 +053038 setConsoleWindow: (state, window) => (state.consoleWindow = window),
Derick Montaguee080a1a2019-12-04 16:30:08 -060039 },
40 actions: {
Yoshie Muranakad624dae2020-09-21 14:35:58 -070041 login({ commit }, { username, password }) {
Derick Montaguea06fe462020-03-11 13:48:42 -050042 commit('authError', false);
Derick Montaguee080a1a2019-12-04 16:30:08 -060043 return api
Ed Tanousebef6ee2023-08-07 18:25:41 -070044 .post('/login', {
45 username: username,
46 password: password,
47 })
Derick Montaguefded0d12019-12-11 06:16:40 -060048 .then(() => commit('authSuccess'))
Derick Montague602e98a2020-10-21 16:20:00 -050049 .catch((error) => {
Derick Montaguefded0d12019-12-11 06:16:40 -060050 commit('authError');
Derick Montaguee080a1a2019-12-04 16:30:08 -060051 throw new Error(error);
52 });
53 },
54 logout({ commit }) {
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080055 api
Derick Montaguefded0d12019-12-11 06:16:40 -060056 .post('/logout', { data: [] })
kirankumarb07b89eed22023-01-12 15:50:30 +053057 .then(() => {
58 commit('setConsoleWindow', false);
59 commit('logout');
60 })
Thang Q. Nguyen780733a2023-04-19 14:27:18 +070061 .then(() => router.push('/login'))
Derick Montague602e98a2020-10-21 16:20:00 -050062 .catch((error) => console.log(error));
Yoshie Muranaka2c98b092020-06-22 13:28:09 -070063 },
Damian Celicoaeb19812022-11-24 02:00:53 +010064 getUserInfo(_, username) {
65 return api
Yoshie Muranaka2c98b092020-06-22 13:28:09 -070066 .get(`/redfish/v1/AccountService/Accounts/${username}`)
Damian Celicoaeb19812022-11-24 02:00:53 +010067 .then(({ data }) => data)
Derick Montague602e98a2020-10-21 16:20:00 -050068 .catch((error) => console.log(error));
Yoshie Muranakad624dae2020-09-21 14:35:58 -070069 },
70 resetStoreState({ state }) {
71 state.authError = false;
72 state.xsrfCookie = Cookies.get('XSRF-TOKEN');
73 state.isAuthenticatedCookie = Cookies.get('IsAuthenticated');
Derick Montague602e98a2020-10-21 16:20:00 -050074 },
75 },
Derick Montaguee080a1a2019-12-04 16:30:08 -060076};
77
78export default AuthenticationStore;