42 lines
989 B
JavaScript
42 lines
989 B
JavaScript
import axios from 'axios';
|
|
import store from '../store';
|
|
import { getApiBaseUrl } from './appConfig.js';
|
|
|
|
// API-Basis-URL - Apache-Proxy für Produktion, direkte Verbindung für lokale Entwicklung
|
|
const getApiBaseURL = () => {
|
|
return getApiBaseUrl();
|
|
};
|
|
|
|
|
|
const apiClient = axios.create({
|
|
baseURL: getApiBaseURL(),
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
});
|
|
|
|
apiClient.interceptors.request.use(config => {
|
|
const user = store.getters.user;
|
|
|
|
if (user && user.authCode) {
|
|
config.headers['userid'] = user.id;
|
|
config.headers['authcode'] = user.authCode; // Kleinschreibung!
|
|
}
|
|
|
|
return config;
|
|
}, error => {
|
|
return Promise.reject(error);
|
|
});
|
|
|
|
apiClient.interceptors.response.use(response => {
|
|
return response;
|
|
}, error => {
|
|
if (error.response && error.response.status === 401) {
|
|
store.dispatch('logout');
|
|
}
|
|
return Promise.reject(error);
|
|
});
|
|
|
|
export default apiClient;
|
|
export { getApiBaseURL };
|