
--- client/resources/api/index.js
+++ client/resources/api/index.js
... | ... | @@ -2,60 +2,63 @@ |
2 | 2 |
import store from "../../views/pages/AppStore"; |
3 | 3 |
|
4 | 4 |
const apiClient = axios.create({ |
5 |
- headers: { |
|
6 |
- 'Content-Type': 'application/json; charset=UTF-8', |
|
7 |
- } |
|
5 |
+ headers: { |
|
6 |
+ 'Content-Type': 'application/json; charset=UTF-8', |
|
7 |
+ } |
|
8 | 8 |
}); |
9 | 9 |
|
10 | 10 |
apiClient.interceptors.request.use( |
11 |
- config => { |
|
12 |
- config.headers.Authorization = store.state.authorization; // 요청 시 AccessToken 추가 |
|
13 |
- return config; |
|
14 |
- }, |
|
15 |
- error => { |
|
16 |
- return Promise.reject(error); |
|
17 |
- } |
|
11 |
+ config => { |
|
12 |
+ config.headers.Authorization = store.state.authorization; // 요청 시 AccessToken 추가 |
|
13 |
+ return config; |
|
14 |
+ }, |
|
15 |
+ error => { |
|
16 |
+ return Promise.reject(error); |
|
17 |
+ } |
|
18 | 18 |
) |
19 | 19 |
|
20 | 20 |
apiClient.interceptors.response.use( |
21 |
- response => { |
|
22 |
- return response; |
|
23 |
- }, |
|
24 |
- async error => { |
|
25 |
- if (error.response.status == 403 && error.response.data.message == '접근 권한이 없습니다.') { |
|
26 |
- window.history.back(); |
|
21 |
+ response => { |
|
22 |
+ console.log('Response Status:', response.status); // 응답 상태 출력 |
|
23 |
+ return response; |
|
24 |
+ }, |
|
25 |
+ async error => { |
|
26 |
+ console.log('Error Response:', error.response.data); // 오류 응답 데이터 출력 |
|
27 |
+ console.log('Error Status:', error.response.status); // 오류 상태 출력 |
|
28 |
+ if (error.response.status == 403 && error.response.data.message == '접근 권한이 없습니다.') { |
|
29 |
+ window.history.back(); |
|
30 |
+ } |
|
31 |
+ const originalReq = error.config; |
|
32 |
+ // 토큰의 만료기간이 끝난경우 |
|
33 |
+ if (error.response.status == 401 && error.response.data.message == '로그인 시간이 만료되었습니다.' && !originalReq._retry) { |
|
34 |
+ originalReq._retry = true; // 재요청 시도(한번만 실행) |
|
35 |
+ try { |
|
36 |
+ const res = await axios.post('/refresh/tknReissue.json', {}); |
|
37 |
+ store.commit('setAuthorization', res.headers.authorization); // 새로 발급 받은 AccessToken 저장 |
|
38 |
+ originalReq.headers.Authorization = store.state.authorization; // 새로 발급 받은 AccessToken을 기존 요청에 추가 |
|
39 |
+ /** jwt토큰 디코딩 **/ |
|
40 |
+ const base64String = store.state.authorization.split('.')[1]; |
|
41 |
+ const base64 = base64String.replace(/-/g, '+').replace(/_/g, '/'); |
|
42 |
+ const jsonPayload = decodeURIComponent(atob(base64).split('').map(c => { |
|
43 |
+ return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); |
|
44 |
+ }).join('')); |
|
45 |
+ const mbr = JSON.parse(jsonPayload); |
|
46 |
+ // const mbr = JSON.parse(decodeURIComponent(escape(window.atob(base64String)))); // jwt claim 추출 |
|
47 |
+ store.commit("setUserNm", mbr.userNm); |
|
48 |
+ store.commit('setRoles', mbr.roles); |
|
49 |
+ /** jwt토큰 디코딩 **/ |
|
50 |
+ return apiClient(originalReq); // 원래 요청 재시도 /pathname + search |
|
51 |
+ } catch (refreshError) { |
|
52 |
+ const redirect = window.location.pathname + window.location.search; |
|
53 |
+ sessionStorage.setItem("redirect", redirect); |
|
54 |
+ alert('세션이 종료되었습니다.\n로그인을 새로 해주세요.'); |
|
55 |
+ store.commit("setStoreReset"); |
|
56 |
+ window.location = '/login.page'; |
|
57 |
+ return Promise.reject(refreshError); |
|
58 |
+ } |
|
59 |
+ } |
|
60 |
+ return Promise.reject(error); |
|
27 | 61 |
} |
28 |
- const originalReq = error.config; |
|
29 |
- // 토큰의 만료기간이 끝난경우 |
|
30 |
- if (error.response.status == 401 && error.response.data.message == 'Token expired' && !originalReq._retry) { |
|
31 |
- originalReq._retry = true; // 재요청 시도(한번만 실행) |
|
32 |
- try { |
|
33 |
- const res = await axios.post('/refresh/tknReissue.json', {}); |
|
34 |
- store.commit('setAuthorization', res.headers.authorization); // 새로 발급 받은 AccessToken 저장 |
|
35 |
- originalReq.headers.Authorization = store.state.authorization; // 새로 발급 받은 AccessToken을 기존 요청에 추가 |
|
36 |
- /** jwt토큰 디코딩 **/ |
|
37 |
- const base64String = store.state.authorization.split('.')[1]; |
|
38 |
- const base64 = base64String.replace(/-/g, '+').replace(/_/g, '/'); |
|
39 |
- const jsonPayload = decodeURIComponent(atob(base64).split('').map(c => { |
|
40 |
- return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); |
|
41 |
- }).join('')); |
|
42 |
- const mbr = JSON.parse(jsonPayload); |
|
43 |
- // const mbr = JSON.parse(decodeURIComponent(escape(window.atob(base64String)))); // jwt claim 추출 |
|
44 |
- store.commit("setUserNm", mbr.userNm); |
|
45 |
- store.commit('setRoles', mbr.roles); |
|
46 |
- /** jwt토큰 디코딩 **/ |
|
47 |
- return apiClient(originalReq); // 원래 요청 재시도 /pathname + search |
|
48 |
- } catch (refreshError) { |
|
49 |
- const redirect = window.location.pathname + window.location.search; |
|
50 |
- sessionStorage.setItem("redirect", redirect); |
|
51 |
- alert('세션이 종료되었습니다.\n로그인을 새로 해주세요.'); |
|
52 |
- store.commit("setStoreReset"); |
|
53 |
- window.location = '/login.page'; |
|
54 |
- return Promise.reject(refreshError); |
|
55 |
- } |
|
56 |
- } |
|
57 |
- return Promise.reject(error); |
|
58 |
- } |
|
59 | 62 |
) |
60 | 63 |
|
61 | 64 |
export default apiClient;(파일 끝에 줄바꿈 문자 없음) |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?