import api from '@/store/api'; import Cookies from 'js-cookie'; import router from '@/router'; const AuthenticationStore = { namespaced: true, state: { authError: false, // для работы локально // // xsrfCookie: true, // isAuthenticatedCookie: true, // // для сборки образа // xsrfCookie: Cookies.get('XSRF-TOKEN'), isAuthenticatedCookie: Cookies.get('IsAuthenticated'), //// }, getters: { authError: (state) => state.authError, isLoggedIn: (state) => { return ( state.xsrfCookie !== undefined || state.isAuthenticatedCookie == 'true' ); }, token: (state) => state.xsrfCookie, }, mutations: { //Расскомментить этот // authSuccess() { //Закомментить этот код authSuccess(state) { state.authError = false; state.xsrfCookie = Cookies.get('XSRF-TOKEN'); }, authError(state, authError = true) { // authError() { state.authError = authError; }, logout(state) { Cookies.remove('XSRF-TOKEN'); Cookies.remove('IsAuthenticated'); localStorage.removeItem('storedUsername'); state.xsrfCookie = undefined; state.isAuthenticatedCookie = undefined; }, }, actions: { // для работы локально // // login({ commit }) { // commit('authError', false); // commit('authSuccess'); // }, // для сборки образа // login({ commit }, { username, password }) { commit('authError', false); return api .post('/login', { data: [username, password] }) .then(() => commit('authSuccess')) .catch((error) => { commit('authError'); throw new Error(error); }); }, ///// logout({ commit }) { api .post('/logout', { data: [] }) .then(() => commit('logout')) .then(() => router.go('/login')) .catch((error) => console.log(error)); }, // для работы локально // // checkPasswordChangeRequired() { // return false; // }, // для сборки образа // checkPasswordChangeRequired(_, username) { api .get(`/redfish/v1/AccountService/Accounts/${username}`) .then(({ data: { PasswordChangeRequired } }) => PasswordChangeRequired) .catch((error) => console.log(error)); }, /////// // для работы локально // // resetStoreState() {}, // для сборки образа // resetStoreState({ state }) { state.authError = false; state.xsrfCookie = Cookies.get('XSRF-TOKEN'); state.isAuthenticatedCookie = Cookies.get('IsAuthenticated'); }, ////// }, }; export default AuthenticationStore;