-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaxios.ts
More file actions
37 lines (34 loc) · 760 Bytes
/
axios.ts
File metadata and controls
37 lines (34 loc) · 760 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
import axios from "axios";
import { useAuthStore } from "~/store/auth";
export const api = axios.create({
baseURL: import.meta.env.VITE_API_URL,
headers: {
"Content-Type": "application/json",
},
});
api.interceptors.request.use(
(config) => {
const jwt = useAuthStore.getState().jwt;
if (jwt) {
config.headers.Authorization = `Bearer ${jwt}`;
}
return config;
},
(error) => {
return Promise.reject(error);
},
);
api.interceptors.response.use(
(response) => {
if (response.status === 401) {
useAuthStore.getState().logout();
}
return response;
},
(error) => {
if (error.response?.status === 401) {
useAuthStore.getState().logout();
}
return Promise.reject(error);
},
);