props/Props/assets/js/services/http.js

24 lines
642 B
JavaScript
Raw Normal View History

import axios from "axios";
let currentAuthorizationInterceptorID = null;
2021-07-13 01:00:17 +00:00
const apiHttp = axios.create({
baseURL: window.location.origin + "/api",
2021-07-13 01:00:17 +00:00
timeout: 3000,
});
function addBearerTokenInterceptor(token) {
2021-07-13 01:00:17 +00:00
currentAuthorizationInterceptorID = apiHttp.interceptors.request.use((config) => {
config.headers.Authorization = `Bearer ${token}`;
return config;
}, (err) => {
console.error(err);
});
}
function removeBearerTokenInterceptor() {
2021-07-13 01:00:17 +00:00
apiHttp.interceptors.request.eject(currentAuthorizationInterceptorID);
}
2021-07-13 01:00:17 +00:00
export { apiHttp, addBearerTokenInterceptor, removeBearerTokenInterceptor };