48 lines
978 B
JavaScript
48 lines
978 B
JavaScript
import axios from "axios";
|
|
import Cookies from "universal-cookie";
|
|
|
|
const cookies = new Cookies();
|
|
const BASE_URL = "https://nobat724.com/";
|
|
|
|
const api = axios.create({
|
|
baseURL: BASE_URL,
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
|
|
api.interceptors.request.use(
|
|
(request) => {
|
|
const token = cookies.get("token");
|
|
const isLoginPage = window.location.pathname === "/login";
|
|
|
|
if (!token) {
|
|
if (!isLoginPage) {
|
|
window.location.pathname = "/login";
|
|
}
|
|
} else {
|
|
if (isLoginPage) {
|
|
window.location.pathname = "/";
|
|
}
|
|
request.headers.authorization = `Bearer ${token}`;
|
|
}
|
|
return request;
|
|
},
|
|
(err) => Promise.reject(err)
|
|
);
|
|
|
|
api.interceptors.response.use(
|
|
(response) => {
|
|
return response.data;
|
|
},
|
|
(error) => {
|
|
if (error.data.status === 400) {
|
|
cookies.remove("token", { path: "/" });
|
|
window.location.pathname = "/login";
|
|
} else {
|
|
}
|
|
}
|
|
);
|
|
|
|
export default api;
|