Skip to content

Commit

Permalink
Merge pull request #52 from Chaem03/feature/#22
Browse files Browse the repository at this point in the history
✨ Feature: 에러페이지 연결
  • Loading branch information
Chaem03 authored Sep 15, 2024
2 parents e62385f + a0ffc36 commit e420008
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
38 changes: 31 additions & 7 deletions src/apis/instance.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,51 @@
import axios from "axios";
import Cookies from 'js-cookie';
const accessToken = Cookies.get('access_token');
import Cookies from "js-cookie";
const accessToken = Cookies.get("access_token");

export const instance = axios.create({
baseURL: import.meta.env.VITE_BASE_URL,
withCredentials: true,
headers: {
Authorization: `Bearer ${Cookies.get('access_token')}`,
Authorization: `Bearer ${Cookies.get("access_token")}`,
},
timeout: 10000, // 10초 타임아웃 설정
});

instance.interceptors.request.use(
(config) => {
const accessToken = Cookies.get('access_token'); // 요청을 보낼 때마다 쿠키에서 액세스 토큰을 가져옵니다.
const accessToken = Cookies.get("access_token"); // 요청을 보낼 때마다 쿠키에서 액세스 토큰을 가져옵니다.
if (accessToken) {
config.headers['Authorization'] = `Bearer ${accessToken}`; // 액세스 토큰이 있으면 헤더에 추가합니다.
config.headers["Authorization"] = `Bearer ${accessToken}`; // 액세스 토큰이 있으면 헤더에 추가합니다.
config.withCredentials = true;
}

return config;
},
(error) => {
return Promise.reject(error);
},
);
}
);

// 응답 인터셉터 추가
instance.interceptors.response.use(
(response) => response,
(error) => {
if (error.response) {
if (error.response.status === 404) {
console.log("404 error: Page not found");
window.location.href = "/error";
}
const errorCode = error.response.data?.errorCode;
if (errorCode === "USER_LOCATION_002") {
console.log(
"USER_LOCATION_002: 사용자와 명소 정보가 일치하지 않습니다."
);
window.location.href = "/error";
}
} else if (error.code === "ECONNABORTED") {
console.log("Request timeout");
window.location.href = "/error";
}
return Promise.reject(error);
}
);
1 change: 1 addition & 0 deletions src/pages/CompletePage/styled.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ export const Button = styled.div`
font-style: normal;
font-weight: 600;
color: #fef7e2;
cursor: pointer;
`;

0 comments on commit e420008

Please sign in to comment.