Newer
Older
import axios from 'axios';
import { AxiosError, AxiosRequestConfig } from 'axios';
import { AuthoriseUser } from '../services/Authorise/Authorise';
const Api = axios.create({
baseURL: 'http://localhost:5267/api/'
});
Api.interceptors.response.use(
(response) => response, // Normal success logic
(error: AxiosError) => {
if (error.request.status === 401) { // If 401 (Unauthorized)
AuthoriseUser().then(() => { // Re-auth
return Api.request(error.config as AxiosRequestConfig); // Redo the request
});
} else {
throw error; // Otherwise throw error as normal
}
}
);
export default Api;