feat: Expo scaffold + auth screens + core screens (#39-#46, #48)
This commit is contained in:
32
services/api.ts
Normal file
32
services/api.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import axios from 'axios';
|
||||
import * as SecureStore from 'expo-secure-store';
|
||||
import { API_URL, STORAGE_KEYS } from '../constants';
|
||||
|
||||
export const api = axios.create({
|
||||
baseURL: API_URL,
|
||||
timeout: 15000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
api.interceptors.request.use(async (config) => {
|
||||
const token = await SecureStore.getItemAsync(STORAGE_KEYS.TOKEN);
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
});
|
||||
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
async (error) => {
|
||||
if (error.response?.status === 401) {
|
||||
await SecureStore.deleteItemAsync(STORAGE_KEYS.TOKEN);
|
||||
await SecureStore.deleteItemAsync(STORAGE_KEYS.USER);
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
export default api;
|
||||
Reference in New Issue
Block a user