import axios from "axios"; import { useEffect } from "react"; import { useSelector, useDispatch } from "react-redux"; import { getTokenID } from "../redux/modules/userSlice"; import Cookies from "js-cookie"; const baseURL = process.env.REACT_APP_BASEURL; axios.defaults.baseURL = `${baseURL}`; const useAxiosInterceptor = () => { const dispatch = useDispatch(); useEffect(() => { const interceptor = axios.interceptors.response.use( (response) => response, async (error) => { const originalRequest = error.config; // If the error status is 401 and the request has not been retried yet if (error.response.status === 401 && !originalRequest._retry) { originalRequest._retry = true; // Attempt to refresh the token try { const response = await axios.get("/refresh", { withCredentials: true }); if (response.status === 200 && response.data?.message?.token) { const newToken = response.data.message.token; dispatch(getTokenID(newToken)); const expirationDate = new Date(); expirationDate.setTime(expirationDate.getTime() + 30 * 24 * 60 * 60 * 1000); // 30 days const expires = "expires=" + expirationDate.toUTCString(); document.cookie = `_secure_ARJ_=${newToken};${expires};path=/`; originalRequest.headers['Authorization'] = `Bearer ${newToken}`; return axios(originalRequest); } } catch (err) { console.error("Error refreshing token", err); localStorage.clear(); const expiredDate = new Date(0).toUTCString(); document.cookie = `_secure_ARJ_=; expires=${expiredDate}; path=/`; document.cookie = `_secure_ref=; expires=${expiredDate}; path=/`; document.cookie = `cookiesAccepted=; expires=${expiredDate}; path=/`; window.location.href = "/login"; } } if (error.response.status === 401 && originalRequest.url === "refresh") { localStorage.clear(); const expiredDate = new Date(0).toUTCString(); document.cookie = `_secure_ARJ_=; expires=${expiredDate}; path=/`; document.cookie = `_secure_ref=; expires=${expiredDate}; path=/`; document.cookie = `cookiesAccepted=; expires=${expiredDate}; path=/`; window.location.href = "/login"; } return Promise.reject(error); } ); return () => { // Cleanup function to remove the interceptor when the component unmounts axios.interceptors.response.eject(interceptor); }; }, [dispatch]); // No need to return anything, the interceptor setup will be handled by useEffect }; export default useAxiosInterceptor;