-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathaxiosInstance.js
More file actions
66 lines (62 loc) · 1.66 KB
/
axiosInstance.js
File metadata and controls
66 lines (62 loc) · 1.66 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { CONFIG } from "@/config/config";
import axios from "axios";
import router from "../router/index";
import LoginUser from "../api/admin/authAPI.js";
const axiosInstance = axios.create({
headers: {
"Content-Type": "multipart/form-data"
},
baseURL: CONFIG.beastRoot
});
axiosInstance.interceptors.request.use(
function(config) {
let userInfo = LoginUser.getUserInfo();
if (userInfo && userInfo.token) {
config.headers["Authorization"] = "Bearer " + userInfo.token;
}
return config;
},
function(error) {
return Promise.reject(error);
}
);
axiosInstance.interceptors.response.use(
function(response) {
return response;
},
function(error) {
let ignoreErrorPagesPath = [
"/auth/login",
"/auth/register",
"/auth/send-otp",
"/auth/reset-password",
"/auth/verify-otp",
"/auth/verify-otp-forget",
"/api/submit/challenge"
];
// For hint errors, pass through the error response
if (error.response && error.response.config.url.includes("/api/info/hint")) {
return Promise.reject(error);
}
if (!error.response) {
router.push("/error/networkerror");
} else if (ignoreErrorPagesPath.includes(error.response.config.url)) {
return error.response;
} else if (error.response.config.url.includes("/api/info/logo")) {
return error;
} else {
switch (error.response.status) {
case 401:
router.push("/error/401");
break;
case 404:
router.push("/error/404");
break;
default:
return error;
}
Promise.reject(error);
}
}
);
export default axiosInstance;