added interceptors.response
This commit is contained in:
@@ -35,7 +35,7 @@ function Login() {
|
||||
const usuario = payload.usuario;
|
||||
|
||||
document.cookie = `token=${token}; path=/; SameSite=Strict`;
|
||||
document.cookie = `usuario=${usuario}; path=/; SameSite=Strict`;
|
||||
document.cookie = `user=${usuario}; path=/; SameSite=Strict`;
|
||||
|
||||
toast.success("Inicio de sesión exitoso");
|
||||
router.push("/Impresiones");
|
||||
|
||||
+42
-7
@@ -2,22 +2,57 @@ import axios from "axios";
|
||||
import Cookies from "js-cookie";
|
||||
import { envConfig } from "./config";
|
||||
|
||||
if (!envConfig.apiUrl) {
|
||||
throw new Error("API URL is not defined in envConfig");
|
||||
}
|
||||
|
||||
const apiClient = axios.create({
|
||||
baseURL: envConfig.apiUrl,
|
||||
timeout: 10000,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
apiClient.interceptors.request.use((config) => {
|
||||
const token = Cookies.get("token");
|
||||
if (token) {
|
||||
if (config.headers && "set" in config.headers) {
|
||||
config.headers.set("Authorization", `Bearer ${token}`);
|
||||
apiClient.interceptors.request.use(
|
||||
(config) => {
|
||||
const token = Cookies.get("token");
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
return config;
|
||||
});
|
||||
);
|
||||
|
||||
apiClient.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response) {
|
||||
const status = error.response.status;
|
||||
|
||||
if (status === 401) {
|
||||
Cookies.remove("token");
|
||||
Cookies.remove("user");
|
||||
|
||||
if (
|
||||
typeof window !== "undefined" &&
|
||||
!window.location.pathname.includes("/")
|
||||
) {
|
||||
window.location.href = "/";
|
||||
}
|
||||
}
|
||||
|
||||
if (status === 403) {
|
||||
window.location.href = "/Impresiones";
|
||||
}
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
export default apiClient;
|
||||
//IO
|
||||
//Mike
|
||||
|
||||
Reference in New Issue
Block a user