37 lines
867 B
TypeScript
37 lines
867 B
TypeScript
import axios from 'axios';
|
|
import * as SecureStore from 'expo-secure-store';
|
|
import { config } from '../constants/config';
|
|
|
|
export const api = axios.create({
|
|
baseURL: config.API_BASE_URL,
|
|
timeout: 10000,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
|
|
// Request interceptor - add auth token
|
|
api.interceptors.request.use(
|
|
async (axiosConfig) => {
|
|
const token = await SecureStore.getItemAsync('auth_token');
|
|
if (token) {
|
|
axiosConfig.headers.Authorization = `Bearer ${token}`;
|
|
}
|
|
return axiosConfig;
|
|
},
|
|
(error) => Promise.reject(error)
|
|
);
|
|
|
|
// Response interceptor - handle errors
|
|
api.interceptors.response.use(
|
|
(response) => response,
|
|
async (error) => {
|
|
if (error.response?.status === 401) {
|
|
await SecureStore.deleteItemAsync('auth_token');
|
|
}
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
export default api;
|