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(_, username) { 65 return api 66 .get(`/redfish/v1/AccountService/Accounts/${username}`) 67 .then(({ data }) => data) 68 .catch((error) => console.log(error)); 69 }, 70 resetStoreState({ state }) { 71 state.authError = false; 72 state.xsrfCookie = Cookies.get('XSRF-TOKEN'); 73 state.isAuthenticatedCookie = Cookies.get('IsAuthenticated'); 74 }, 75 }, 76}; 77 78export default AuthenticationStore; 79