import { useState, useEffect } from 'react'; import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, signOut, onAuthStateChanged } from 'firebase/auth'; import { getDatabase, ref, set, get } from 'firebase/database'; import { initializeApp } from 'firebase/app'; import { ShoppingCart, User, LogOut, Menu, X } from 'lucide-react'; const firebaseConfig = { apiKey: "AIzaSyAQ2FHAqG5fQMvYvYlTCldZJXd7EAmDwfw", authDomain: "comers-2.firebaseapp.com", databaseURL: "https://comers-2-default-rtdb.firebaseio.com", projectId: "comers-2", storageBucket: "comers-2.firebasestorage.app", messagingSenderId: "843430528469", appId: "1:843430528469:web:e386d5434e1bbe02ab695c" }; const app = initializeApp(firebaseConfig); const auth = getAuth(app); const db = getDatabase(app); export default function App() { const [user, setUser] = useState(null); const [products, setProducts] = useState([]); const [cart, setCart] = useState([]); const [isLoginForm, setIsLoginForm] = useState(true); const [isMenuOpen, setIsMenuOpen] = useState(false); const [loading, setLoading] = useState(true); useEffect(() => { const unsubscribe = onAuthStateChanged(auth, (user) => { setUser(user); if (user) { loadUserCart(user.uid); } setLoading(false); }); fetchProducts(); return () => unsubscribe(); }, []); const fetchProducts = async () => { try { const response = await fetch('https://fakestoreapi.com/products'); const data = await response.json(); setProducts(data); } catch (error) { console.error('Error fetching products:', error); } }; const loadUserCart = async (userId) => { try { const cartRef = ref(db, `carts/${userId}`); const snapshot = await get(cartRef); if (snapshot.exists()) { setCart(snapshot.val()); } } catch (error) { console.error('Error loading cart:', error); } }; const saveUserCart = async (userId, cart) => { try { await set(ref(db, `carts/${userId}`), cart); } catch (error) { console.error('Error saving cart:', error); } }; const handleAuth = async (email, password, isLogin) => { try { if (isLogin) { await signInWithEmailAndPassword(auth, email, password); } else { await createUserWithEmailAndPassword(auth, email, password); } } catch (error) { console.error('Authentication error:', error); alert(error.message); } }; const handleLogout = async () => { try { await signOut(auth); setCart([]); } catch (error) { console.error('Logout error:', error); } }; const addToCart = (product) => { const newCart = [...cart, product]; setCart(newCart); if (user) { saveUserCart(user.uid, newCart); } }; const AuthForm = () => { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); return (

{isLoginForm ? 'Login' : 'Register'}

{ e.preventDefault(); handleAuth(email, password, isLoginForm); }}> setEmail(e.target.value)} className="w-full p-3 mb-4 border rounded" required /> setPassword(e.target.value)} className="w-full p-3 mb-4 border rounded" required />
); }; if (loading) { return (
); } return (
{/* Header */}

E-Commerce Store

{user ? ( <> Welcome, {user.email} ) : ( )}
{cart.length > 0 && ( {cart.length} )}
{/* Mobile Menu */} {isMenuOpen && (
{user ? ( <>
Welcome, {user.email}
) : (
Please login
)}
)} {/* Main Content */}
{!user ? ( ) : (
{products.map((product) => (
{product.title}

{product.title}

{product.description}

${product.price}
))}
)}
{/* Footer */}
); }