auto logout function when there is authentication error #104
-
Hello, everyone! |
Beta Was this translation helpful? Give feedback.
Answered by
creative2115
Sep 11, 2024
Replies: 1 comment 3 replies
-
Hello, @creative2113 import axios from "axios";
const axiosHandler = axios.create({
baseURL: process.env.REACT_APP_BASE_URL,
headers: {
Accept: "application/json",
},
});
axiosHandler.interceptors.request.use(
(config) => {
const token = localStorage.getItem("token");
if (token) {
config.headers["Authorization"] = "Bearer " + token;
}
return config;
},
(error) => {
Promise.reject(error);
}
);
//axiosHandler.interceptors.response
export default axiosHandler; in error case, we can add code for logout action in the Error handler field of interceptor. |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
creative2113
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, @creative2113
we can use interceptor axiosHandler for solving this problem.
Here is an example code:
in error case, we can add code for logout action in the Error handler field of interceptor.
This cod…