
File name
Commit message
Commit date
05-22
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
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: window.sessionStorage,
storage: strgMode === 'S' ? window.sessionStorage : window.localStorage,
paths: ['loginMode', 'authorization', 'mbrId', 'mbrNm', 'roles', 'contextPath', 'pageAuth']
})],
state: {
authorization: null,
// refresh: null,
loginMode: lgnMode || 'J',
userType: "portal",
menu: null,
path: null,
roles: [{authority: "ROLE_NONE"}],
pageAuth: null,
contextPath: ctx || null,
},
getters: {
getAuthorization: function () {},
// getRefresh: function () {},
getMbrNm: function () {},
getRoles: function () {},
getLoginMode: state => state.loginMode,
},
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.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;
},
},
actions: {
async logout({ commit }) {
try {
const ctx = this.state.contextPath; // 캐시 초기화 전 contextPath 저장
const admPath = this.state.path?.includes("/adm") // 캐시 초기화 전 경로 구분 (true: 관리자 페이지, false: 사용자 페이지)
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 토큰 쿠키
'oauth_refresh_token', // OAuth 리프레시 토큰
'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. 로그인 페이지로 이동 (OAuth 관련 파라미터 제거)
const cleanUrl = admPath ?
ctx + "/cmslogin.page" :
ctx + "/login.page";
// URL에서 OAuth 관련 파라미터 제거
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;
}