|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
891 bytes
|
| Line | |
|---|
| 1 | import axios from 'axios';
|
|---|
| 2 |
|
|---|
| 3 | const API_URL = process.env.REACT_APP_API_URL || 'http://localhost:8080/api';
|
|---|
| 4 |
|
|---|
| 5 | const apiClient = axios.create({
|
|---|
| 6 | baseURL: API_URL,
|
|---|
| 7 | headers: {
|
|---|
| 8 | 'Content-Type': 'application/json',
|
|---|
| 9 | },
|
|---|
| 10 | });
|
|---|
| 11 |
|
|---|
| 12 | // Request interceptor to add JWT token
|
|---|
| 13 | apiClient.interceptors.request.use(
|
|---|
| 14 | config => {
|
|---|
| 15 | const token = localStorage.getItem('token');
|
|---|
| 16 | if (token) {
|
|---|
| 17 | config.headers.Authorization = `Bearer ${token}`;
|
|---|
| 18 | console.log('✅ Token added to request:', config.url);
|
|---|
| 19 | } else {
|
|---|
| 20 | console.warn('⚠️ No token found for request:', config.url);
|
|---|
| 21 | }
|
|---|
| 22 | return config;
|
|---|
| 23 | },
|
|---|
| 24 | error => Promise.reject(error)
|
|---|
| 25 | );
|
|---|
| 26 |
|
|---|
| 27 | // Error handling interceptor
|
|---|
| 28 | apiClient.interceptors.response.use(
|
|---|
| 29 | response => response,
|
|---|
| 30 | error => {
|
|---|
| 31 | console.error('API Error:', error.response?.data || error.message);
|
|---|
| 32 | return Promise.reject(error);
|
|---|
| 33 | }
|
|---|
| 34 | );
|
|---|
| 35 |
|
|---|
| 36 | export default apiClient;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.