|
import axios from 'axios'; |
|
import { createGlobalState } from 'react-global-hooks'; |
|
|
|
export const isAuthorizedState = createGlobalState(false); |
|
|
|
export const apiClient = axios.create(); |
|
|
|
|
|
apiClient.interceptors.request.use(config => { |
|
const token = localStorage.getItem('AI_TOOLKIT_AUTH'); |
|
if (token) { |
|
config.headers['Authorization'] = `Bearer ${token}`; |
|
} |
|
return config; |
|
}); |
|
|
|
|
|
apiClient.interceptors.response.use( |
|
response => response, |
|
error => { |
|
|
|
if (error.response && error.response.status === 401) { |
|
|
|
localStorage.removeItem('AI_TOOLKIT_AUTH'); |
|
isAuthorizedState.set(false); |
|
} |
|
|
|
|
|
return Promise.reject(error); |
|
}, |
|
); |
|
|