Fix Auth Errors
This commit is contained in:
@@ -6,6 +6,8 @@ import React from 'react';
|
||||
import { Auth0Provider as BaseAuth0Provider, useAuth0 } from '@auth0/auth0-react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { apiClient, setAuthReady } from '../api/client';
|
||||
import { createIndexedDBAdapter } from '../utils/indexeddb-storage';
|
||||
import { setAuthInitialized } from './auth-gate';
|
||||
|
||||
interface Auth0ProviderProps {
|
||||
children: React.ReactNode;
|
||||
@@ -38,8 +40,8 @@ export const Auth0Provider: React.FC<Auth0ProviderProps> = ({ children }) => {
|
||||
scope: 'openid profile email offline_access',
|
||||
}}
|
||||
onRedirectCallback={onRedirectCallback}
|
||||
// Mobile Safari/ITP: use localstorage + refresh tokens to avoid third‑party cookie silent auth failures
|
||||
cacheLocation="localstorage"
|
||||
// Mobile-optimized: use IndexedDB for better mobile compatibility
|
||||
cache={createIndexedDBAdapter()}
|
||||
useRefreshTokens={true}
|
||||
useRefreshTokensFallback={true}
|
||||
>
|
||||
@@ -162,8 +164,17 @@ const TokenInjector: React.FC<{ children: React.ReactNode }> = ({ children }) =>
|
||||
let interceptorId: number | undefined;
|
||||
|
||||
if (isAuthenticated) {
|
||||
// Enhanced pre-warm token cache for mobile devices
|
||||
// Enhanced pre-warm token cache for mobile devices with IndexedDB wait
|
||||
const initializeToken = async () => {
|
||||
// Wait for IndexedDB to be ready first
|
||||
try {
|
||||
const { indexedDBStorage } = await import('../utils/indexeddb-storage');
|
||||
await indexedDBStorage.waitForReady();
|
||||
console.log('[Auth] IndexedDB storage is ready');
|
||||
} catch (error) {
|
||||
console.warn('[Auth] IndexedDB not ready, proceeding anyway:', error);
|
||||
}
|
||||
|
||||
// Give Auth0 more time to fully initialize on mobile devices
|
||||
const isMobile = /Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
|
||||
const initDelay = isMobile ? 500 : 100; // Longer delay for mobile
|
||||
@@ -177,6 +188,7 @@ const TokenInjector: React.FC<{ children: React.ReactNode }> = ({ children }) =>
|
||||
console.log('[Mobile Auth] Token pre-warming successful');
|
||||
setRetryCount(0);
|
||||
setAuthReady(true);
|
||||
setAuthInitialized(true); // Signal that auth is fully ready
|
||||
} else {
|
||||
console.error('[Mobile Auth] Failed to acquire token after retries - will retry on API calls');
|
||||
setRetryCount(prev => prev + 1);
|
||||
@@ -189,9 +201,13 @@ const TokenInjector: React.FC<{ children: React.ReactNode }> = ({ children }) =>
|
||||
|
||||
initializeToken();
|
||||
|
||||
// Add token to all API requests with enhanced error handling
|
||||
// Add token to all API requests with enhanced error handling and IndexedDB wait
|
||||
interceptorId = apiClient.interceptors.request.use(async (config) => {
|
||||
try {
|
||||
// Ensure IndexedDB is ready before getting tokens
|
||||
const { indexedDBStorage } = await import('../utils/indexeddb-storage');
|
||||
await indexedDBStorage.waitForReady();
|
||||
|
||||
const token = await getTokenWithRetry();
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
@@ -209,6 +225,7 @@ const TokenInjector: React.FC<{ children: React.ReactNode }> = ({ children }) =>
|
||||
} else {
|
||||
setRetryCount(0);
|
||||
setAuthReady(false);
|
||||
setAuthInitialized(false); // Reset auth gate when not authenticated
|
||||
}
|
||||
|
||||
// Cleanup function to remove interceptor
|
||||
|
||||
Reference in New Issue
Block a user