-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaxios.ts
More file actions
41 lines (35 loc) · 909 Bytes
/
axios.ts
File metadata and controls
41 lines (35 loc) · 909 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
import axios from "axios";
import useAuthStore from "@/store/useAuthStore";
const instance = axios.create({
baseURL: import.meta.env.VITE_API_BASE_URL,
timeout: 10000,
headers: {
"Content-Type": "application/json"
}
});
// 인증 절차가 필요한 API는 authInstance로 HTTP요청
const authInstance = axios.create({
baseURL: import.meta.env.VITE_API_BASE_URL,
timeout: 10000,
headers: {
"Content-Type": "application/json"
}
});
// authInstance 헤더에 accessToken 추가하는 로직
authInstance.interceptors.request.use(
(config) => {
if (!config.headers) {
config.headers = {};
}
const { accessToken } = useAuthStore.getState();
if (accessToken) {
config.headers.Authorization = `Bearer ${accessToken}`;
}
return config;
},
(error) => {
return Promise.reject(error);
}
);
export default instance;
export { authInstance };