"use client"; import React, { useState, useEffect } from "react"; import { Modal, ModalContent, ModalHeader, ModalBody, ModalFooter, Button, Input, addToast, } from "@heroui/react"; import { getAuth, signInWithEmailAndPassword, signOut, onAuthStateChanged, User } from "firebase/auth"; import firebaseApp from "@/config/firebase"; export const AdminAuth = () => { const [isOpen, setIsOpen] = useState(false); const [user, setUser] = useState(null); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [loading, setLoading] = useState(false); const auth = getAuth(firebaseApp()); useEffect(() => { const unsubscribe = onAuthStateChanged(auth, (currentUser) => { setUser(currentUser); }); return () => unsubscribe(); }, [auth]); const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); try { await signInWithEmailAndPassword(auth, email, password); addToast({ title: "Welcome Titas", description: "Admin access granted.", color: "success", }); setIsOpen(false); setEmail(""); setPassword(""); } catch (error) { addToast({ title: "Access Denied", description: "Invalid credentials.", color: "danger", }); } finally { setLoading(false); } }; const handleLogout = async () => { try { await signOut(auth); addToast({ title: "Signed Out", description: "Admin session ended.", color: "warning", }); setIsOpen(false); } catch (error) { console.error("Logout error", error); } }; return ( <> {(onClose) => ( <> {user ? "Admin Profile" : "Secure Login"} {user ? (

Logged in as

{user.email}

) : (
setEmail(e.target.value)} isRequired classNames={{ input: "outline-none" }} /> setPassword(e.target.value)} isRequired classNames={{ input: "outline-none" }} />
)}
{user ? ( ) : ( )} )}
); };