fix
Browse filesFiles changed (2)
56 unmodified lines5758596061622 unmodified lines6566676869707172737475767778795 unmodified lines8586878889905 unmodified lines969798991001011021031041051061071081091101 unmodified line112113114115116117118119120121122123124125126127128129130131132597 unmodified lines73073173256 unmodified lines}}, [messages]);// Check auth on mount and handle OAuth callbackuseEffect(() => {checkAuth();2 unmodified lines// initializeOAuth already handles this, but we call checkAuth to sync stateconst urlParams = new URLSearchParams(window.location.search);if (urlParams.get('session')) {// OAuth callback - check auth after a brief delay to let initializeOAuth completesetTimeout(() => checkAuth(), 100);}}, []); // Only run once on mount// Listen for storage changes (e.g., logout from another tab)useEffect(() => {const handleStorageChange = (e: StorageEvent) => {if (e.key === 'hf_oauth_token' || e.key === 'hf_user_info') {checkAuth();}};5 unmodified lines// Listen for window focus (user returns to tab after OAuth redirect)useEffect(() => {const handleFocus = () => {checkAuth();};5 unmodified linesconst authenticated = checkIsAuthenticated();setIsAuthenticated(authenticated);// Make sure API client has the tokenif (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 pollingif (!username) {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 errorsif (error.code !== 'ECONNABORTED' &&error.code !== 'ECONNRESET' &&!error.message?.includes('socket hang up') &&!error.message?.includes('timeout')) {console.error('Failed to get username:', error);}}}}} else {// Not authenticated - clear usernameif (username) {setUsername(null);}}};597 unmodified lines);}56 unmodified lines5758596061626364652 unmodified lines686970717273747576777879808182838485865 unmodified lines92939495969798995 unmodified lines1051061071081091101111121131141151161171181191201 unmodified line122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161597 unmodified lines75976076176256 unmodified lines}}, [messages]);// Track if we've attempted to fetch username to avoid repeated failuresconst usernameFetchAttemptedRef = useRef(false);// Check auth on mount and handle OAuth callbackuseEffect(() => {checkAuth();2 unmodified lines// initializeOAuth already handles this, but we call checkAuth to sync stateconst urlParams = new URLSearchParams(window.location.search);if (urlParams.get('session')) {// OAuth callback - reset username fetch attempt and check auth after a brief delayusernameFetchAttemptedRef.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 oneuseEffect(() => {const handleStorageChange = (e: StorageEvent) => {if (e.key === 'hf_oauth_token' || e.key === 'hf_user_info') {// Reset username fetch attempt when storage changesusernameFetchAttemptedRef.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 linesconst authenticated = checkIsAuthenticated();setIsAuthenticated(authenticated);// Make sure API client has the token or clears itif (authenticated) {const token = getStoredToken();if (token) {apiClient.setToken(token);// 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 pollingif (!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// Connection errors mean backend isn't available, which is OK for client-side authconst 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 errorsconsole.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 statesetIsAuthenticated(false);if (username) {setUsername(null);}usernameFetchAttemptedRef.current = false;}} else {// Not authenticated - clear username and reset fetch attemptapiClient.setToken(null);if (username) {setUsername(null);}usernameFetchAttemptedRef.current = false;}};597 unmodified lines);}
76 unmodified lines7778798081828384858687888976 unmodified lines}async getModels(): Promise<Model[]> {const response = await this.client.get<Model[]>('/api/models');return response.data;}async getLanguages(): Promise<{ languages: Language[] }> {const response = await this.client.get<{ languages: Language[] }>('/api/languages');return response.data;}async getAuthStatus(): Promise<AuthStatus> {76 unmodified lines777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013176 unmodified lines}async getModels(): Promise<Model[]> {try {const response = await this.client.get<Model[]>('/api/models');return response.data;} catch (error: any) {// Handle connection errors gracefullyconst 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 throwingconsole.warn('Backend not available, cannot load models');return [];}// Re-throw other errorsthrow error;}}async getLanguages(): Promise<{ languages: Language[] }> {try {const response = await this.client.get<{ languages: Language[] }>('/api/languages');return response.data;} catch (error: any) {// Handle connection errors gracefullyconst 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 throwingconsole.warn('Backend not available, using default languages');return { languages: ['html', 'gradio', 'transformers.js', 'streamlit', 'comfyui', 'react'] };}// Re-throw other errorsthrow error;}}async getAuthStatus(): Promise<AuthStatus> {
frontend/src/app/page.tsx+42-12
frontend/src/lib/api.ts+46-4