import { useState, useEffect } from "react"; import "./App.css"; import { AuthProvider } from "./contexts/AuthContext"; import { ChatScreen } from "./components/ChatScreen"; import { LoginScreen } from "./components/LoginScreen"; import { conversationService } from "./api/conversationService"; const AppContainer = () => { const [isAuthenticated, setAuthenticated] = useState(false); const [isChecking, setIsChecking] = useState(true); useEffect(() => { const checkAuth = async () => { const accessToken = localStorage.getItem("access_token"); const refreshToken = localStorage.getItem("refresh_token"); // No tokens at all, not authenticated if (!accessToken && !refreshToken) { setIsChecking(false); setAuthenticated(false); return; } // Try to verify token by making a request try { await conversationService.getAllConversations(); // If successful, user is authenticated setAuthenticated(true); } catch (error) { // Token is invalid or expired console.error("Authentication check failed:", error); localStorage.removeItem("access_token"); localStorage.removeItem("refresh_token"); setAuthenticated(false); } finally { setIsChecking(false); } }; checkAuth(); }, []); // Show loading state while checking authentication if (isChecking) { return (
Loading...
); } return ( <> {isAuthenticated ? ( ) : ( )} ); }; const App = () => { return ( ); }; export default App;