// frontend/services/api.js import axios from 'axios'; import * as SecureStore from…
- User
// frontend/services/api.js
import axios from 'axios'; import * as SecureStore from 'expo-secure-store'; import { Alert } from 'react-native'; import Constants from 'expo-constants'; // Import expo-constants
// Access the API base URL from Expo's config const { apiBaseUrl } = Constants.manifest.extra;
// Log the API base URL to verify it console.log('API Base URL:', apiBaseUrl);
// Create an Axios instance const api = axios.create({ baseURL: apiBaseUrl, // Use the config value headers: { 'Content-Type': 'application/json', }, });
// Request interceptor to attach the token to headers api.interceptors.request.use( async (config) => { try { const token = await SecureStore.getItemAsync('authToken'); // Ensure this key matches AuthContext if (token) { config.headers.Authorization =
Bearer ${token}; } } catch (error) { console.error('Error retrieving token:', error); Alert.alert('Error', 'Failed to retrieve authentication token.'); } return config; }, (error) => { return Promise.reject(error); } );// Response interceptor for global error handling (optional) api.interceptors.response.use( (response) => response, (error) => { if (error.response) { // Server responded with a status other than 2xx console.error('API Error:', error.response.data); // Optionally, handle specific status codes here } else if (error.request) { // Request was made but no response received console.error('Network Error: No response received', error.request); } else { // Something else caused the error console.error('Error:', error.message); } return Promise.reject(error); } );
export default api;
ERROR TypeError: Cannot read property 'extra' of null, js engine: hermes ERROR Invariant Violation: "main" has not been registered. This can happen if:
- Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
- A module failed to load due to an error and
AppRegistry.registerComponentwasn't called., js engine: hermes
- Assistant