30 lines
678 B
TypeScript
30 lines
678 B
TypeScript
import axios from 'axios';
|
|
import { getAccessToken } from '../auth/oidc';
|
|
import { triggerLoginRedirect } from '../auth/sessionRedirect';
|
|
import { getEnv } from '../utils/env';
|
|
|
|
const api = axios.create({
|
|
baseURL: getEnv('VITE_API_URL') || '',
|
|
timeout: 30000,
|
|
});
|
|
|
|
api.interceptors.request.use(async (config) => {
|
|
const token = await getAccessToken();
|
|
if (token) {
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|
}
|
|
return config;
|
|
});
|
|
|
|
api.interceptors.response.use(
|
|
(response) => response,
|
|
async (error) => {
|
|
if (error.response?.status === 401) {
|
|
await triggerLoginRedirect();
|
|
}
|
|
return Promise.reject(error);
|
|
},
|
|
);
|
|
|
|
export default api;
|