Extract Axios Config

This commit is contained in:
sosokker 2023-11-11 22:59:07 +07:00
parent 056e08e012
commit ff84f020e3
3 changed files with 44 additions and 52 deletions

View File

@ -1,45 +1,5 @@
import axios from 'axios';
// Create an Axios instance with common configurations
const axiosInstance = axios.create({
baseURL: 'http://127.0.0.1:8000/api/',
timeout: 5000,
headers: {
'Authorization': "Bearer " + localStorage.getItem('access_token'),
'Content-Type': 'application/json',
'accept': 'application/json',
}
});
// Add a response interceptor to handle token refresh on 401 Unauthorized errors
axiosInstance.interceptors.response.use(
response => response,
error => {
const originalRequest = error.config;
const refresh_token = localStorage.getItem('refresh_token');
// Check if the error is due to Unauthorized (401) and a refresh token is available
if (error.response.status === 401 && error.response.statusText === "Unauthorized" && refresh_token !== "undefined") {
return axiosInstance
.post('/token/refresh/', { refresh: refresh_token })
.then((response) => {
// Update access and refresh tokens
localStorage.setItem('access_token', response.data.access);
localStorage.setItem('refresh_token', response.data.refresh);
// Update the authorization header with the new access token
axiosInstance.defaults.headers['Authorization'] = "Bearer " + response.data.access;
originalRequest.headers['Authorization'] = "Bearer " + response.data.access;
return axiosInstance(originalRequest); // Retry the original request
})
.catch(err => {
console.log('Interceptors error: ', err);
});
}
return Promise.reject(error);
}
);
import axiosInstance from './configs/AxiosConfig';
// Function for user login
const apiUserLogin = (data) => {

View File

@ -1,15 +1,5 @@
import axios from 'axios';
// Create an Axios instance with common configurations
const axiosInstance = axios.create({
baseURL: 'http://127.0.0.1:8000/api/',
timeout: 5000,
headers: {
'Authorization': "Bearer " + localStorage.getItem('access_token'),
'Content-Type': 'application/json',
'accept': 'application/json',
}
});
import axiosInstance from './configs/AxiosConfig';
export const fetchTodoTasks = () => {
return axiosInstance

View File

@ -0,0 +1,42 @@
import axios from 'axios';
const axiosInstance = axios.create({
baseURL: 'http://127.0.0.1:8000/api/',
timeout: 5000,
headers: {
'Authorization': "Bearer " + localStorage.getItem('access_token'),
'Content-Type': 'application/json',
'accept': 'application/json',
},
});
// handling token refresh on 401 Unauthorized errors
axiosInstance.interceptors.response.use(
response => response,
error => {
const originalRequest = error.config;
const refresh_token = localStorage.getItem('refresh_token');
// Check if the error is due to 401 and a refresh token is available
if (error.response.status === 401 && error.response.statusText === "Unauthorized" && refresh_token !== "undefined") {
return axiosInstance
.post('/token/refresh/', { refresh: refresh_token })
.then((response) => {
localStorage.setItem('access_token', response.data.access);
axiosInstance.defaults.headers['Authorization'] = "Bearer " + response.data.access;
originalRequest.headers['Authorization'] = "Bearer " + response.data.access;
return axiosInstance(originalRequest);
})
.catch(err => {
console.log('Interceptors error: ', err);
});
}
return Promise.reject(error);
}
);
export default axiosInstance;