File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
import axios from 'axios';
import store from "../../views/pages/AppStore";
import VueCookies from "vue-cookies";
const apiClient = axios.create({
headers: {
'Content-Type' : 'application/json; charset=UTF-8',
},
});
apiClient.interceptors.request.use(
config => {
config.headers.Authorization = store.state.authorization; // 요청 시 AccessToken 추가
return config;
},
error => {
return Promise.reject(error);
}
)
apiClient.interceptors.response.use(
response => {
return response;
},
async error => {
if(error.response.status == 403 && error.response.data.message == '접근 권한이 없습니다.') {
window.history.back();
}
const originalReq = error.config;
// 토큰의 만료기간이 끝난경우
if(error.response.status == 401 && error.response.data.message == 'Token expired' && !originalReq._retry) {
originalReq._retry = true; // 재요청 시도(한번만 실행)
let isRefresh = true;
if(originalReq.url === '/event/findAllEvent.json') {
if(store.state.routerFlag){
store.commit('setRouterFlag', false);
} else {
isRefresh = false;
}
}
try {
const res = await axios.post('/refresh/tokenReissue.json', {}, {
headers: {
// 'refresh' : store.state.refresh // refresh token
'refresh' : VueCookies.get('refresh'),
'isRefresh' : isRefresh
}
});
store.commit('setAuthorization', res.headers.authorization); // 새로 발급 받은 AccessToken 저장
originalReq.headers.Authorization = store.state.authorization; // 새로 발급 받은 AccessToken을 기존 요청에 추가
/** jwt토큰 복호화 **/
const base64String = store.state.authorization.split('.')[1];
const mbr = JSON.parse(decodeURIComponent(escape(window.atob(base64String)))); // jwt claim 추출
store.commit("setMbrId", mbr.mbrId);
store.commit("setMbrNm", mbr.mbrEncptFlnm);
store.commit("setAuthrtNm", mbr.authrtNm);
store.commit("setAuthrtTypeLvl", mbr.authrtTypeLvl);
store.commit('setRoles', mbr.roles);
/** jwt토큰 복호화 **/
return apiClient(originalReq); // 원래 요청 재시도 /pathname + search
} catch(refreshError) {
const redirect = window.location.pathname + window.location.search;
sessionStorage.setItem("redirect", redirect);
//alert('세션이 종료되었습니다.\n로그인을 새로 해주세요.');
store.commit("setStoreReset");
// store.commit("setAuthorization", null);
// store.commit("setRefresh", null);
// store.commit("setMbrNm", null);
// store.commit('setRoles', [{authority: "ROLE_NONE"}]);
// store.commit('setPageAuth', null);
window.location = '/';
return Promise.reject(refreshError);
}
}
return Promise.reject(error);
}
)
export default apiClient;