Spaces:
Sleeping
Sleeping
update
Browse filesFiles changed (2)
12345650 unmodified lines57585960616263646566676 unmodified lines74757677787980818283848586878889'use client';import { useState, useEffect } from 'react';import { flushSync } from 'react-dom';import Header from '@/components/Header';import LandingPage from '@/components/LandingPage';50 unmodified lines}}, [messages]);useEffect(() => {checkAuth();// Check auth status every second to catch OAuth redirectsconst interval = setInterval(checkAuth, 1000);return () => clearInterval(interval);}, []);const checkAuth = async () => {6 unmodified linesif (token) {apiClient.setToken(token);// Get username from auth statustry {const authStatus = await apiClient.getAuthStatus();if (authStatus.username) {setUsername(authStatus.username);}} catch (error) {console.error('Failed to get username:', error);}}}};12345650 unmodified lines5758596061626364656667686970717273747576777879808182838485868788899091929394956 unmodified lines102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132'use client';import { useState, useEffect, useRef } from 'react';import { flushSync } from 'react-dom';import Header from '@/components/Header';import LandingPage from '@/components/LandingPage';50 unmodified lines}}, [messages]);// Check auth on mount and handle OAuth callbackuseEffect(() => {checkAuth();// Check for OAuth callback in URL (handles ?session=token)// 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();}};window.addEventListener('storage', handleStorageChange);return () => window.removeEventListener('storage', handleStorageChange);}, []);// Listen for window focus (user returns to tab after OAuth redirect)useEffect(() => {const handleFocus = () => {checkAuth();};window.addEventListener('focus', handleFocus);return () => window.removeEventListener('focus', handleFocus);}, []);const checkAuth = async () => {6 unmodified linesif (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) {setUsername(authStatus.username);}} 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);}}};
48 unmodified lines49505152535434 unmodified lines89909192939495969748 unmodified linesheaders: {'Content-Type': 'application/json',},});// Add auth token to requests if available34 unmodified linestry {const response = await this.client.get<AuthStatus>('/api/auth/status');return response.data;} catch (error) {return {authenticated: false,message: 'Not authenticated',};}48 unmodified lines4950515253545534 unmodified lines9091929394959697989910010110210310410510610710810948 unmodified linesheaders: {'Content-Type': 'application/json',},timeout: 10000, // 10 second timeout to prevent hanging connections});// Add auth token to requests if available34 unmodified linestry {const response = await this.client.get<AuthStatus>('/api/auth/status');return response.data;} catch (error: any) {// Silently handle connection errors - don't spam consoleif (error.code === 'ECONNABORTED' || error.code === 'ECONNRESET' || error.message?.includes('socket hang up')) {// Connection error - backend may not be readyreturn {authenticated: false,username: null,message: 'Connection error',};}// For other errors, return not authenticatedreturn {authenticated: false,username: null,message: 'Not authenticated',};}
frontend/src/app/page.tsx+54-11
frontend/src/lib/api.ts+13-1