Commit e6f3e052 authored by Yoon, Daeki's avatar Yoon, Daeki 😅
Browse files

api error 발생을 axios interceptor 이용 처리함

parent 8d00247e
import { useEffect } from "react";
import { NavigateFunction, useLocation } from "react-router-dom";
import customAxios from "./axios.config";
export const useAxiosInterceptor = (navigate: NavigateFunction) => {
const location = useLocation();
useEffect(() => {
const responseInterceptor = customAxios.interceptors.response.use(
(response) => response,
(error) => {
if (error.response.status === 401) {
navigate("/login", {
replace: true,
state: { from: location.pathname },
});
}
return Promise.reject(error);
}
);
return () => {
customAxios.interceptors.response.eject(responseInterceptor);
};
}, []);
};
import React from "react"; import React from "react";
import { Outlet } from "react-router-dom"; import { Outlet, useNavigate } from "react-router-dom";
import { useAxiosInterceptor } from "../apis";
import { AuthProvider } from "../auth"; import { AuthProvider } from "../auth";
import { Header } from "../commons"; import { Header } from "../commons";
export const BaseLayout = () => { export const BaseLayout = () => {
const navigate = useNavigate();
useAxiosInterceptor(navigate);
return ( return (
<AuthProvider> <AuthProvider>
<Header /> <Header />
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment