1import api from '@/store/api'; 2import Cookies from 'js-cookie'; 3import router from '@/router'; 4 5const AuthenticationStore = { 6 namespaced: true, 7 state: { 8 consoleWindow: null, 9 authError: false, 10 xsrfCookie: Cookies.get('XSRF-TOKEN'), 11 isAuthenticatedCookie: Cookies.get('IsAuthenticated'), 12 }, 13 getters: { 14 consoleWindow: (state) => state.consoleWindow, 15 authError: (state) => state.authError, 16 isLoggedIn: (state) => { 17 return ( 18 state.xsrfCookie !== undefined || state.isAuthenticatedCookie == 'true' 19 ); 20 }, 21 token: (state) => state.xsrfCookie, 22 }, 23 mutations: { 24 authSuccess(state) { 25 state.authError = false; 26 state.xsrfCookie = Cookies.get('XSRF-TOKEN'); 27 }, 28 authError(state, authError = true) { 29 state.authError = authError; 30 }, 31 logout(state) { 32 Cookies.remove('XSRF-TOKEN'); 33 Cookies.remove('IsAuthenticated'); 34 localStorage.removeItem('storedUsername'); 35 state.xsrfCookie = undefined; 36 state.isAuthenticatedCookie = undefined; 37 }, 38 setConsoleWindow: (state, window) => (state.consoleWindow = window), 39 }, 40 actions: { 41 login({ commit }, { username, password }) { 42 commit('authError', false); 43 return api 44 .post('/login', { 45 username: username, 46 password: password, 47 }) 48 .then(() => commit('authSuccess')) 49 .catch((error) => { 50 commit('authError'); 51 throw new Error(error); 52 }); 53 }, 54 logout({ commit }) { 55 api 56 .post('/logout', { data: [] }) 57 .then(() => { 58 commit('setConsoleWindow', false); 59 commit('logout'); 60 }) 61 .then(() => router.push('/login')) 62 .catch((error) => console.log(error)); 63 }, 64 getUserInfo({ commit }, username) { 65 return api 66 .get(`/redfish/v1/AccountService/Accounts/${username}`) 67 .then(({ data }) => { 68 commit('global/setPrivilege', data.RoleId, { root: true }); 69 return data; 70 }) 71 .catch((error) => console.log(error)); 72 }, 73 resetStoreState({ state }) { 74 state.authError = false; 75 state.xsrfCookie = Cookies.get('XSRF-TOKEN'); 76 state.isAuthenticatedCookie = Cookies.get('IsAuthenticated'); 77 }, 78 }, 79}; 80 81export default AuthenticationStore; 82