Fix Auth Errors
This commit is contained in:
@@ -3,25 +3,70 @@
|
||||
* @ai-context Handles auth tokens and error responses
|
||||
*/
|
||||
|
||||
import axios, { AxiosInstance, InternalAxiosRequestConfig } from 'axios';
|
||||
import axios, { InternalAxiosRequestConfig } from 'axios';
|
||||
import toast from 'react-hot-toast';
|
||||
|
||||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || '/api';
|
||||
|
||||
export const apiClient: AxiosInstance = axios.create({
|
||||
baseURL: API_BASE_URL,
|
||||
timeout: 10000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
// Will be replaced by createQueuedAxios below
|
||||
|
||||
// Auth readiness flag to avoid noisy 401 toasts during mobile auth initialization
|
||||
let authReady = false;
|
||||
export const setAuthReady = (ready: boolean) => { authReady = ready; };
|
||||
export const isAuthReady = () => authReady;
|
||||
|
||||
// Request interceptor for auth token with mobile debugging
|
||||
// Create a wrapper around axios that queues requests until auth is ready
|
||||
const createQueuedAxios = () => {
|
||||
const queuedClient = axios.create({
|
||||
baseURL: API_BASE_URL,
|
||||
timeout: 10000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
// Store original methods
|
||||
const originalRequest = queuedClient.request.bind(queuedClient);
|
||||
const originalGet = queuedClient.get.bind(queuedClient);
|
||||
const originalPost = queuedClient.post.bind(queuedClient);
|
||||
const originalPut = queuedClient.put.bind(queuedClient);
|
||||
const originalDelete = queuedClient.delete.bind(queuedClient);
|
||||
const originalPatch = queuedClient.patch.bind(queuedClient);
|
||||
|
||||
// Create wrapper function for auth queue checking
|
||||
const wrapWithAuthQueue = (originalMethod: any, methodName: string) => {
|
||||
return async (...args: any[]) => {
|
||||
try {
|
||||
const { queueRequest, isAuthInitialized } = await import('../auth/auth-gate');
|
||||
|
||||
if (!isAuthInitialized()) {
|
||||
console.log(`[API Client] Queuing ${methodName} request until auth ready`);
|
||||
return queueRequest(() => originalMethod(...args));
|
||||
}
|
||||
|
||||
return originalMethod(...args);
|
||||
} catch (error) {
|
||||
console.warn(`[API Client] Auth gate import failed for ${methodName}, proceeding with request:`, error);
|
||||
return originalMethod(...args);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// Override all HTTP methods
|
||||
queuedClient.request = wrapWithAuthQueue(originalRequest, 'REQUEST');
|
||||
queuedClient.get = wrapWithAuthQueue(originalGet, 'GET');
|
||||
queuedClient.post = wrapWithAuthQueue(originalPost, 'POST');
|
||||
queuedClient.put = wrapWithAuthQueue(originalPut, 'PUT');
|
||||
queuedClient.delete = wrapWithAuthQueue(originalDelete, 'DELETE');
|
||||
queuedClient.patch = wrapWithAuthQueue(originalPatch, 'PATCH');
|
||||
|
||||
return queuedClient;
|
||||
};
|
||||
|
||||
// Replace the basic axios instance with the queued version
|
||||
export const apiClient = createQueuedAxios();
|
||||
|
||||
// Request interceptor for token injection and logging
|
||||
apiClient.interceptors.request.use(
|
||||
async (config: InternalAxiosRequestConfig) => {
|
||||
// Token will be added by Auth0 wrapper
|
||||
|
||||
Reference in New Issue
Block a user