Created
March 31, 2024 11:45
-
-
Save Praiseike/d31aaccf160e70e4e89f9b2c30f2bce3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import axios from "axios"; | |
| export const API_BASE_URL = | |
| "https://rapyd-staging-server-b442066858cb.herokuapp.com/api/v1/"; | |
| const axiosInstance = axios.create({ | |
| baseURL: API_BASE_URL, | |
| }); | |
| // request interceptor for adding bearer tokens | |
| axiosInstance.interceptors.request.use( | |
| (config) => { | |
| const token = localStorage.getItem("token"); | |
| if (token) config.headers["Authorization"] = `Bearer ${token}`; | |
| return config; | |
| }, | |
| (error) => { | |
| return Promise.reject(error); | |
| } | |
| ); | |
| // response interceptor for confirming authorization | |
| axiosInstance.interceptors.response.use( | |
| (response) => response, | |
| (error) => { | |
| if (error.message !== "Network Error") { | |
| if (error?.response?.status === 401) { | |
| // alert("You need to login"); | |
| console.log("access denied 401 redirecting"); | |
| window.location.assign("/login"); | |
| } | |
| } | |
| return Promise.reject(error); | |
| } | |
| ); | |
| export default axiosInstance; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment