-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaxiosInstance.ts
More file actions
42 lines (36 loc) · 975 Bytes
/
axiosInstance.ts
File metadata and controls
42 lines (36 loc) · 975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import axios from 'axios';
import { HTTP_STATUS } from '@/api/constant/httpStatus';
const axiosInstance = axios.create({
baseURL: import.meta.env.VITE_API_BASE_URL,
headers: {
'Content-Type': 'application/json',
},
withCredentials: true,
});
axiosInstance.interceptors.request.use(
(config) => {
const token = localStorage.getItem('accessToken');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
},
(error) => Promise.reject(error),
);
// 응답 인터셉터
axiosInstance.interceptors.response.use(
(response) => response,
(error) => {
if (error.response) {
const { status } = error.response;
if (status === HTTP_STATUS.UNAUTHORIZED) {
console.warn('인증 실패');
}
if (status === HTTP_STATUS.INTERNAL_SERVER_ERROR) {
console.error('서버 오류가 발생');
}
}
return Promise.reject(error);
},
);
export default axiosInstance;