// Auth0-aware API client wrapper // This file provides helper functions that automatically inject Auth0 tokens import { api } from './client'; // Token provider function - will be set by App.tsx let tokenProvider: (() => Promise) | null = null; export function setTokenProvider(provider: () => Promise) { tokenProvider = provider; } // Wrapper that automatically adds Auth0 token to requests async function makeAuthenticatedRequest( requestFn: (headers: HeadersInit) => Promise ): Promise { if (!tokenProvider) { // Fallback to localStorage for non-Auth0 flows (shouldn't happen) const token = localStorage.getItem('authToken'); const headers = token ? { Authorization: `Bearer ${token}` } : {}; return requestFn(headers); } try { const token = await tokenProvider(); return requestFn({ Authorization: `Bearer ${token}` }); } catch (error) { console.error('Failed to get access token:', error); throw error; } } // Re-export all API methods (they already handle authorization headers) export { api, vipApi, driverApi, scheduleApi, authApi } from './client'; // Note: The original ApiClient in client.ts already reads from localStorage // and adds the Authorization header. Auth0 SDK stores tokens in localStorage // by default (cacheLocation: "localstorage"), so everything should work seamlessly. // // For more advanced use cases (e.g., using Auth0's memory cache or handling // token refresh explicitly), you would use the tokenProvider approach above.