Added configured axios instance with 401 interceptor, refactored all requests to use axios instance
This commit is contained in:
1 parent
fb292d23a6
commit
10c8751c6b
8 files changed
+56
-40
No files matched your search
@@ -0,0 +1,27 @@
|
||||
import axios from "axios";
|
||||
import { clearAuthState } from "../Features/Auth/authSlice";
|
||||
const BASE_URL = import.meta.env.VITE_APP_API_BASE_URL;
|
||||
|
||||
let store;
|
||||
|
||||
export const injectStore = (s) => {
|
||||
store = s;
|
||||
};
|
||||
|
||||
const axiosInstance = axios.create({
|
||||
baseURL: BASE_URL,
|
||||
});
|
||||
|
||||
axiosInstance.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
console.error(error);
|
||||
if (error.response && error.response.status === 401) {
|
||||
console.log("Invalid token revoked");
|
||||
store.dispatch(clearAuthState());
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
export default axiosInstance;
|
||||
Reference in new issue
Block a user