-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from leejaeseong11/fix-axios
Fix : axios-interceptor 적용
- Loading branch information
Showing
14 changed files
with
149 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import axios from "axios" | ||
|
||
var apiClient = null; | ||
apiClient = axios.create({ | ||
// baseURL: "//" + "192.168.1.116:8881/codesparring", | ||
baseURL: import.meta.env.VITE_BACK_URL, | ||
withCredentials: true | ||
}); | ||
|
||
apiClient.interceptors.request.use( | ||
function (config) { | ||
config.headers["Content-Type"] = "application/json;"; | ||
config.headers["Authorization"] = `Bearer ${sessionStorage.getItem('accessToken')}`; | ||
return config; | ||
} | ||
) | ||
|
||
apiClient.interceptors.response.use(res => { | ||
if (!(res.status === 200 || res.status === 201 || res.status === 204)) throw new Error(); | ||
if (res.data.errors) throw new Error(res.data.errors); | ||
|
||
return res; | ||
} | ||
|
||
,async (error) => { | ||
if (error.response?.status === 401) { | ||
alert("로그인이 필요합니다") | ||
window.location.href='/login'; | ||
} | ||
if (error.response?.status === 403 || error.response?.status === 500) { | ||
console.error("403 Forbidden 에러 발생 :", error.response) | ||
alert("접근 권한이 없습니다") | ||
window.history.back(); | ||
} | ||
return Promise.reject(error); | ||
} | ||
|
||
// ,async (error) => { | ||
// const err = error; | ||
// console.log("==================//response================"); | ||
// console.log(error); | ||
// console.log("==================response//================"); | ||
// if (err.response?.status === 401) { | ||
// const data = err.response?.data; | ||
// console.log(err.response); | ||
// console.log("data===="); | ||
// console.log(data); | ||
|
||
// //if (토큰 재발행) { | ||
// const param = { | ||
// 'refreshToken' : '', | ||
// 'accessToken':`Bearer ${sessionStorage.getItem('accessToken')}` | ||
// } | ||
|
||
// console.log(param); | ||
// // const token = await axios.get('http://127.0.0.1:8881/codesparring/auth/reissue?refreshToken='+param.refreshToken + "&accessToken="+param.accessToken, { | ||
// // headers: { | ||
// // "Content-Type" : "application/json" | ||
// // } | ||
// // }); | ||
// const token = await axios.post('http://127.0.0.1:8881/codesparring/auth/reissue', param,{ | ||
// headers: { | ||
// "Content-Type" : "application/json" | ||
// } | ||
// }); | ||
// console.log("token======="); | ||
// console.log(token); | ||
// sessionStorage.setItem("accessToken", token); | ||
// err.config.headers = { | ||
// "Content-Type": "application/json", | ||
// Authorization: `Bearer ${token}`, | ||
// }; | ||
// // 중단된 요청을 새로운 토큰으로 재전송 | ||
// const originalResponse = await axios.request(err.config); | ||
// return originalResponse.data; | ||
// // } | ||
// } | ||
|
||
// return Promise.reject(error); | ||
// } | ||
); | ||
|
||
export {apiClient}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.