fix
Browse filesFiles changed (2)
56 unmodified lines5758596061626364652 unmodified lines6869706869717273747576777879808182838485865 unmodified lines92939495969798995 unmodified lines105106107991081091101111121131051061071141151161171181191201 unmodified line122123124115116117118119125126127128129130131132133134135136137138139140141142143144145146147148149150151152126153154155156157158159160161597 unmodified lines75976076176256 unmodified lines } }, [messages]);
// Track if we've attempted to fetch username to avoid repeated failures const usernameFetchAttemptedRef = useRef(false);
// Check auth on mount and handle OAuth callback useEffect(() => { checkAuth();2 unmodified lines // initializeOAuth already handles this, but we call checkAuth to sync state const urlParams = new URLSearchParams(window.location.search); if (urlParams.get('session')) { // OAuth callback - check auth after a brief delay to let initializeOAuth complete setTimeout(() => checkAuth(), 100); // OAuth callback - reset username fetch attempt and check auth after a brief delay usernameFetchAttemptedRef.current = false; setTimeout(() => checkAuth(), 200); } }, []); // Only run once on mount
// Listen for storage changes (e.g., logout from another tab) // Note: storage events only fire in OTHER tabs, not the current one useEffect(() => { const handleStorageChange = (e: StorageEvent) => { if (e.key === 'hf_oauth_token' || e.key === 'hf_user_info') { // Reset username fetch attempt when storage changes usernameFetchAttemptedRef.current = false; checkAuth(); } };5 unmodified lines // Listen for window focus (user returns to tab after OAuth redirect) useEffect(() => { const handleFocus = () => { // Reset username fetch attempt on focus (user might have logged in elsewhere) usernameFetchAttemptedRef.current = false; checkAuth(); };
5 unmodified lines const authenticated = checkIsAuthenticated(); setIsAuthenticated(authenticated); // Make sure API client has the token // Make sure API client has the token or clears it if (authenticated) { const token = getStoredToken(); if (token) { apiClient.setToken(token); // Get username from auth status (only if we don't have it yet) // This is a one-time fetch, not polling if (!username) { // Get username from auth status (only if we don't have it yet and haven't failed) // This is a one-time fetch per session, not polling if (!username && !usernameFetchAttemptedRef.current) { usernameFetchAttemptedRef.current = true; try { const authStatus = await apiClient.getAuthStatus(); if (authStatus.username) {1 unmodified line } } catch (error: any) { // Silently handle connection errors - don't spam console // Only log non-connection errors if (error.code !== 'ECONNABORTED' && error.code !== 'ECONNRESET' && !error.message?.includes('socket hang up') && !error.message?.includes('timeout')) { // Connection errors mean backend isn't available, which is OK for client-side auth const isConnectionError = error.code === 'ECONNABORTED' || error.code === 'ECONNRESET' || error.code === 'ECONNREFUSED' || error.message?.includes('socket hang up') || error.message?.includes('timeout') || error.message?.includes('Network Error') || error.response?.status === 503 || error.response?.status === 502; if (!isConnectionError) { // Only log non-connection errors console.error('Failed to get username:', error); } // Reset attempt flag so we can try again later (e.g., when backend comes up) usernameFetchAttemptedRef.current = false; } } } else { // Token missing but authenticated flag is true - clear state setIsAuthenticated(false); if (username) { setUsername(null); } usernameFetchAttemptedRef.current = false; } } else { // Not authenticated - clear username // Not authenticated - clear username and reset fetch attempt apiClient.setToken(null); if (username) { setUsername(null); } usernameFetchAttemptedRef.current = false; } };
597 unmodified lines );}
76 unmodified lines77787980808181828384858687888990919293949596979899100101102103104105106858610710810911011111211311411511611711811912012112212312412512612712812913013176 unmodified lines }
async getModels(): Promise<Model[]> { try { const response = await this.client.get<Model[]>('/api/models'); return response.data; const response = await this.client.get<Model[]>('/api/models'); return response.data; } catch (error: any) { // Handle connection errors gracefully const isConnectionError = error.code === 'ECONNABORTED' || error.code === 'ECONNRESET' || error.code === 'ECONNREFUSED' || error.message?.includes('socket hang up') || error.message?.includes('timeout') || error.message?.includes('Network Error') || error.response?.status === 503 || error.response?.status === 502; if (isConnectionError) { // Backend is not available - return empty array instead of throwing console.warn('Backend not available, cannot load models'); return []; } // Re-throw other errors throw error; } }
async getLanguages(): Promise<{ languages: Language[] }> { try { const response = await this.client.get<{ languages: Language[] }>('/api/languages'); return response.data; const response = await this.client.get<{ languages: Language[] }>('/api/languages'); return response.data; } catch (error: any) { // Handle connection errors gracefully const isConnectionError = error.code === 'ECONNABORTED' || error.code === 'ECONNRESET' || error.code === 'ECONNREFUSED' || error.message?.includes('socket hang up') || error.message?.includes('timeout') || error.message?.includes('Network Error') || error.response?.status === 503 || error.response?.status === 502; if (isConnectionError) { // Backend is not available - return default languages instead of throwing console.warn('Backend not available, using default languages'); return { languages: ['html', 'gradio', 'transformers.js', 'streamlit', 'comfyui', 'react'] }; } // Re-throw other errors throw error; } }
async getAuthStatus(): Promise<AuthStatus> {frontend/src/app/page.tsx+42-12
frontend/src/lib/api.ts+46-4