import { createStore } from "vuex"; import createPersistedState from "vuex-persistedstate"; import { logOutProc } from "../../resources/api/logOut" export default createStore({ plugins: [createPersistedState()], state: { authorization: null, // refresh: null, userType: "portal", menu: null, path: null, roles: [{authority: "ROLE_NONE"}], pageAuth: null, contextPath: null, }, getters: { getAuthorization: function () {}, // getRefresh: function () {}, getMbrNm: function () {}, getRoles: function () {}, }, mutations: { setAuthorization(state, newValue) { state.authorization = newValue; }, // setRefresh(state, newValue) { // state.refresh = newValue; // }, setMbrNm(state, newValue) { state.mbrNm = newValue; }, setMbrId(state, newValue) { state.mbrId = newValue; }, setRoles(state, newValue) { state.roles = newValue; }, setUserType(state, newValue) { state.userType = newValue; }, setMenu(state, newValue) { state.menu = newValue; }, setPath(state, newValue) { state.path = newValue; }, setStoreReset(state) { state.authorization = null; // state.refresh = null; state.mbrNm = null; state.mbrId = null; state.roles = [{authority: "ROLE_NONE"}]; state.menu = null; state.pageAuth = null; state.contextPath = null; }, setPageAuth(state, newValue) { state.pageAuth = newValue; }, setMenuList(state, menuList) { state.menuList = menuList; }, setContextPath(state, ctx) { state.contextPath = ctx; } }, actions: { async logout({ commit }) { try { const ctx = this.state.contextPath; // 캐시 초기화 전 contextPath 저장 const res = await logOutProc(); alert(res.data.message); if (res.status == 200) { // 1. 상태 초기화 commit("setStoreReset"); // 2. 로컬스토리지와 세션스토리지 초기화 localStorage.clear(); sessionStorage.clear(); // 3. 쿠키 삭제 document.cookie = "refresh=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"; document.cookie = "Authorization=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"; // 4. 로그인 페이지로 이동 window.location = ctx + "/login.page"; } } catch(error) { const errorData = error.response.data; if (errorData.message != null && errorData.message != "") { alert(error.response.data.message); } else { alert("에러가 발생했습니다.\n관리자에게 문의해주세요."); } } }, setUserType({ commit }, userType) { commit("setUserType", userType); }, setMenu({ commit }, menu) { commit("setMenu", menu); }, setPath({ commit }, path) { commit("setPath", path); }, setPageAuth({ commit }, pageAuth) { commit("setPageAuth", pageAuth); }, setStoreReset({commit}) { commit("setStoreReset"); }, }, });