import { useState } from "react"; import { useNavigate } from "react-router-dom"; import { supabase } from "@/integrations/supabase/client"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { useToast } from "@/components/ui/use-toast"; export const AdminLogin = () => { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [isLoading, setIsLoading] = useState(false); const navigate = useNavigate(); const { toast } = useToast(); const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); setIsLoading(true); try { const { data: { user }, error } = await supabase.auth.signInWithPassword({ email, password, }); if (error) throw error; if (user) { const { data: isAdmin, error: adminError } = await supabase.rpc('is_admin', { user_id: user.id }); if (adminError || !isAdmin) { throw new Error("Not authorized as admin"); } toast({ title: "Login successful", description: "Welcome to the admin area", }); navigate("/admin"); } } catch (error) { console.error("Login error:", error); toast({ title: "Login failed", description: "Please check your credentials and try again", variant: "destructive", }); } finally { setIsLoading(false); } }; return (

Admin Login

setEmail(e.target.value)} required />
setPassword(e.target.value)} required />
); };