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 const [isAuthenticated, setIsAuthenticated] = useState(false); const [user, setUser] = useState(null); // UI state for login/signup form const [isSignUp, setIsSignUp] = useState(false); const [formData, setFormData] = useState({ name: '', ucid: '', email: '', phone: '', password: '', }); const [showPassword, setShowPassword] = useState(false); const [showImage, setShowImage] = useState(true); const [error, setError] = useState(''); // Fake users database const fakeUsers = [ { email: 'john1@ucalgary.ca', password: 'password123', name: 'John Doe' }, { email: 'jane@ucalgary.ca', password: 'password456', name: 'Jane Smith' } ]; // 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 handleInputChange = (e) => { const { id, value } = e.target; setFormData(prev => ({ ...prev, [id]: value })); // Clear any error when user starts typing again if (error) setError(''); }; const handleLogin = (e) => { e.preventDefault(); // Validate email and password if (!formData.email || !formData.password) { setError('Email and password are required'); return; } if (isSignUp) { // Handle Sign Up console.log('Sign Up Form Data:', formData); // Simulate saving new user to database const newUser = { name: formData.name, email: formData.email, ucid: formData.ucid, phone: formData.phone }; // Set authenticated user setUser(newUser); setIsAuthenticated(true); console.log('New user registered:', newUser); } else { // Handle Login console.log('Login Attempt:', { email: formData.email, password: formData.password }); // Check against fake user database const foundUser = fakeUsers.find( user => user.email === formData.email && user.password === formData.password ); if (foundUser) { // Set authenticated user setUser({ name: foundUser.name, email: foundUser.email }); setIsAuthenticated(true); console.log('Login successful for:', foundUser.name); } else { setError('Invalid email or password'); console.log('Login failed: Invalid credentials'); } } }; const handleLogout = () => { setIsAuthenticated(false); setUser(null); console.log('User logged out'); // Reset form data setFormData({ name: '', ucid: '', email: '', phone: '', password: '', }); }; const toggleAuthMode = () => { setIsSignUp(!isSignUp); setError(''); // Clear errors when switching modes // Reset form data when switching modes setFormData({ name: '', ucid: '', email: '', phone: '', password: '', }); }; // 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;