import { createStore } from "vuex"; import createPersistedState from "vuex-persistedstate"; import { logOutProc } from "../../resources/api/logOut" let globalStore = null; // 전역 스토어 설정 함수 export function setGlobalStore(store) { globalStore = store; } // 전역 스토어 호출 함수 export function getGlobalStore() { return globalStore; } export default function createAppStore(ctx, strgMode, lgnMode) { const store = createStore({ plugins: [createPersistedState({ storage: strgMode === 'S' ? window.sessionStorage : window.localStorage, paths: ['loginMode', 'authorization', 'mbrId', 'mbrNm', 'roles', 'contextPath', 'pageAuth'] })], state: { authorization: null, loginMode: lgnMode || 'J', userType: "portal", menu: null, path: null, roles: [{authority: "ROLE_NONE"}], pageAuth: null, contextPath: ctx || null, }, getters: { getAuthorization: function () {}, getMbrNm: function () {}, getRoles: function () {}, getLoginMode: state => state.loginMode, // 로그인 상태 확인 isLoggedIn: (state) => { if (state.loginMode === 'J') { return !!state.authorization && !!state.mbrNm; } else if (state.loginMode === 'S') { return !!state.mbrNm; } return false; }, }, mutations: { setAuthorization(state, newValue) { state.authorization = 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.loginMode = 'J'; 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; // 메뉴트리 펼치기 const flattenMenus = (menus) => { const result = []; for (const menu of menus) { result.push(menu); if (menu.childList?.length) { result.push(...flattenMenus(menu.childList)); } } return result; } const flattenedMenuList = flattenMenus(menuList); state.flatMenuList = flattenedMenuList; }, setContextPath(state, ctx) { state.contextPath = ctx; }, setLoginMode(state, value) { state.loginMode = value; }, // 로그인 정보 일괄 설정 setLoginInfo(state, { mbrNm, mbrId, roles, authorization, loginMode }) { state.mbrNm = mbrNm; state.mbrId = mbrId; state.roles = roles || [{authority: "ROLE_NONE"}]; if (loginMode) { state.loginMode = loginMode; } // JWT 모드일 때만 authorization 저장 if (state.loginMode === 'J' && authorization) { state.authorization = authorization; } else if (state.loginMode === 'S') { state.authorization = null; // 세션 모드에서는 토큰 불필요 } }, }, actions: { async logout({ commit }) { try { const ctx = this.state.contextPath; const admPath = this.state.path?.includes("/adm") const loginMode = this.state.loginMode || localStorage.getItem('loginMode') || 'J'; // 로그인 모드에 따른 처리 if (loginMode === 'J') { // JWT 방식인 경우만 서버 API 호출 try { const res = await logOutProc(); if (res.data.message) { alert(res.data.message); } } catch (error) { console.log(error); // API 에러가 발생해도 클라이언트는 정리 } } // 1. 상태 초기화 commit("setStoreReset"); // 2. 로컬스토리지와 세션스토리지 초기화 localStorage.clear(); sessionStorage.clear(); // 3. 모든 가능한 쿠키 삭제 (OAuth 관련 포함) const cookiesToDelete = [ 'refresh', 'Authorization', 'JSESSIONID', 'oauth_access_token', 'oauth_refresh_token', 'SESSION' ]; cookiesToDelete.forEach(cookieName => { document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`; document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=${window.location.hostname};`; }); // 4. 로그인 페이지로 이동 const cleanUrl = admPath ? ctx + "/cmslogin.page" : ctx + "/login.page"; window.history.replaceState({}, document.title, cleanUrl); window.location.href = cleanUrl; } catch(error) { console.error("로그아웃 처리 중 오류:", error); // 에러가 발생해도 클라이언트 상태는 정리 commit("setStoreReset"); localStorage.clear(); sessionStorage.clear(); const ctx = this.state.contextPath; const admPath = this.state.path?.includes("/adm"); const errorData = error.response?.data; if (errorData?.message) { alert(errorData.message); } else { console.log("로그아웃 처리 중 예상치 못한 오류 발생"); } const cleanUrl = admPath ? ctx + "/cmslogin.page" : ctx + "/login.page"; window.location.href = cleanUrl; } }, 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"); }, }, }); setGlobalStore(store); return store; }