From 160d4f3b51b4d80e64d8d0acbc832cb54dd831bc Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Sat, 11 Jul 2026 13:53:50 +0000 Subject: [PATCH] Changes Co-authored-by: wolfoftyreso-debug <250630591+wolfoftyreso-debug@users.noreply.github.com> --- src/pages/Admin.tsx | 84 ++++++++++++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 24 deletions(-) diff --git a/src/pages/Admin.tsx b/src/pages/Admin.tsx index 3cf9fb3..e3bcbb5 100644 --- a/src/pages/Admin.tsx +++ b/src/pages/Admin.tsx @@ -8,50 +8,51 @@ import { OrdersTable } from "@/components/admin/OrdersTable"; import { CustomersTable } from "@/components/admin/CustomersTable"; import { PaymentsTable } from "@/components/admin/PaymentsTable"; import { LeadsGenerator } from "@/components/admin/LeadsGenerator"; -import { Package, Users, CreditCard, LogOut, ArrowLeft, Sparkles } from "lucide-react"; +import { AuthDialog } from "@/components/AuthDialog"; +import { Package, Users, CreditCard, LogOut, ArrowLeft, Sparkles, Lock } from "lucide-react"; import { Skeleton } from "@/components/ui/skeleton"; +type AuthState = "loading" | "signed_out" | "not_admin" | "admin"; + export default function Admin() { const navigate = useNavigate(); - const [loading, setLoading] = useState(true); - const [isAuthenticated, setIsAuthenticated] = useState(false); + const [state, setState] = useState("loading"); + const [authOpen, setAuthOpen] = useState(false); useEffect(() => { - const checkAuth = async () => { - const { data: { session } } = await supabase.auth.getSession(); - if (!session) { - navigate("/"); + const check = async (session: any) => { + if (!session?.user) { + setState("signed_out"); return; } - setIsAuthenticated(true); - setLoading(false); + const { data, error } = await supabase + .from("user_roles") + .select("role") + .eq("user_id", session.user.id) + .eq("role", "admin") + .maybeSingle(); + if (error) console.error("Role check failed:", error); + setState(data ? "admin" : "not_admin"); }; - const { data: { subscription } } = supabase.auth.onAuthStateChange((event, session) => { - if (!session) { - navigate("/"); - } + const { data: { subscription } } = supabase.auth.onAuthStateChange((_e, session) => { + check(session); }); - - checkAuth(); - + supabase.auth.getSession().then(({ data }) => check(data.session)); return () => subscription.unsubscribe(); - }, [navigate]); + }, []); const handleLogout = async () => { await supabase.auth.signOut(); - navigate("/"); }; - if (loading) { + if (state === "loading") { return (
- {[...Array(4)].map((_, i) => ( - - ))} + {[...Array(4)].map((_, i) => )}
@@ -59,8 +60,43 @@ export default function Admin() { ); } - if (!isAuthenticated) { - return null; + if (state === "signed_out") { + return ( + <> +
+
+ +

Admin-inloggning krävs

+

+ Denna sida är privat. Logga in med ditt admin-konto för att se ordrar och annan intern information. +

+
+ + +
+
+
+ + + ); + } + + if (state === "not_admin") { + return ( +
+
+ +

Ingen behörighet

+

+ Ditt konto har inte tillgång till admin-sidan. Endast ägaren kan se den här sidan. +

+
+ + +
+
+
+ ); } return (