import { useState, useEffect } from "react"; import { BrowserRouter as Router, Routes, Route, Navigate, } from "react-router-dom"; import Navbar from "./components/Navbar"; import Home from "./pages/Home"; import Settings from "./pages/Settings"; import Selling from "./pages/Selling"; import Transactions from "./pages/Transactions"; import Favorites from "./pages/Favorites"; import ProductDetail from "./pages/ProductDetail"; function App() { // Authentication state - initialize from localStorage if available const [isAuthenticated, setIsAuthenticated] = useState(() => { return localStorage.getItem("isAuthenticated") === "true"; }); const [user, setUser] = useState(() => { const savedUser = localStorage.getItem("user"); return savedUser ? JSON.parse(savedUser) : null; }); // UI state for login/signup form const [isSignUp, setIsSignUp] = useState(false); const [showImage, setShowImage] = useState(true); const [error, setError] = useState(""); const [isLoading, setIsLoading] = useState(false); // Auto-hide image on smaller screens useEffect(() => { const handleResize = () => { if (window.innerWidth < 768) { setShowImage(false); } else { setShowImage(true); } }; // Initial check handleResize(); // Listen for window resize window.addEventListener("resize", handleResize); // Cleanup return () => window.removeEventListener("resize", handleResize); }, []); const handleSubmit = async (e) => { e.preventDefault(); setIsLoading(true); setError(""); // Get form data directly from the form const formData = new FormData(e.target); const formValues = Object.fromEntries(formData.entries()); // Validate email and password if (!formValues.email || !formValues.password) { setError("Email and password are required"); setIsLoading(false); return; } try { if (isSignUp) { // Handle Sign Up console.log("Sign Up Form Data:", formValues); // You could add API endpoint for registration here // For now, we'll just simulate a successful registration const newUser = { name: formValues.name, email: formValues.email, ucid: formValues.ucid, phone: formValues.phone, }; // Set authenticated user setUser(newUser); setIsAuthenticated(true); // Save to localStorage to persist across refreshes localStorage.setItem("isAuthenticated", "true"); localStorage.setItem("user", JSON.stringify(newUser)); console.log("New user registered:", newUser); } else { // Handle Login with API console.log("Login Attempt:", { email: formValues.email, password: formValues.password, }); // Make API call to localhost:3030/find_user const response = await fetch("http://localhost:3030/find_user", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ email: formValues.email, password: formValues.password, }), }); if (!response.ok) { throw new Error("Network response was not ok"); } const userData = await response.json(); if (userData && userData.found) { // Create user object const userObj = { name: userData.name || userData.email, email: userData.email, // Add any other user data returned from the API }; // Set authenticated user with data from API setUser(userObj); setIsAuthenticated(true); // Save to localStorage to persist across refreshes localStorage.setItem("isAuthenticated", "true"); localStorage.setItem("user", JSON.stringify(userObj)); console.log("Login successful for:", userData.email); } else { // Show error message for invalid credentials setError("Invalid email or password"); console.log("Login failed: Invalid credentials"); } } } catch (err) { console.error("Authentication error:", err); setError("Authentication failed. Please try again later."); } finally { setIsLoading(false); } }; const handleLogout = () => { // Clear authentication state setIsAuthenticated(false); setUser(null); // Clear localStorage localStorage.removeItem("isAuthenticated"); localStorage.removeItem("user"); console.log("User logged out"); }; const toggleAuthMode = () => { setIsSignUp(!isSignUp); setError(""); // Clear errors when switching modes }; // Login component const LoginComponent = () => (
{/* Image Section - Automatically hidden on mobile */} {showImage && (
auth illustration
)} {/* Auth Form Section */}

{isSignUp ? "Create Account" : "Welcome Back"}

{isSignUp ? "Set up your new account" : "Sign in to your account"}

{error && (
{error}
)}
{/* Name field - only for signup */} {isSignUp && (
)} {isSignUp && (
)}
{isSignUp && (
)}

{isSignUp ? "Already have an account?" : "Don't have an account?"}{" "}

); // Protected route component const ProtectedRoute = ({ children }) => { if (!isAuthenticated) { return ; } return children; }; return (
{/* Only show navbar when authenticated */} {isAuthenticated && ( )} {/* Public routes */} : } /> {/* Protected routes */}
} /> } />
} />
} />
} />
} /> {/* Redirect to login for any unmatched routes */} } />
); } export default App;