diff --git a/src/App.tsx b/src/App.tsx index 5db6ef4..717b475 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,5 +1,5 @@ -import React from "react"; +import React, { useEffect, useState } from "react"; import { Toaster } from "@/components/ui/toaster"; import { Toaster as Sonner } from "@/components/ui/sonner"; import { TooltipProvider } from "@/components/ui/tooltip"; @@ -24,6 +24,15 @@ const queryClient = new QueryClient({ }); const App = () => { + const [isAutenticado, setIsAutenticado] = useState(null); + + useEffect(() => { + // Verifica se o usuário está autenticado + const autenticado = localStorage.getItem('autenticado') === 'true'; + const userId = localStorage.getItem('userId'); + setIsAutenticado(autenticado && !!userId); + }, []); + return ( @@ -32,11 +41,29 @@ const App = () => { - {/* Rota inicial redireciona para autenticação ou dashboard */} - } /> + {/* Rota inicial redireciona para dashboard ou autenticação */} + + ) : ( + + ) + } + /> - {/* Rota de autenticação */} - } /> + {/* Rota de autenticação - redireciona para transações se já estiver autenticado */} + + ) : ( + + ) + } + /> {/* Rotas protegidas que exigem autenticação */} { const [isAutenticado, setIsAutenticado] = useState(null); const { toast } = useToast(); + const location = useLocation(); useEffect(() => { // Verifica se o usuário está autenticado const autenticado = localStorage.getItem('autenticado') === 'true'; - setIsAutenticado(autenticado); + const userId = localStorage.getItem('userId'); + setIsAutenticado(autenticado && !!userId); - if (!autenticado) { + if (!autenticado || !userId) { toast({ title: "Acesso restrito", description: "Faça login para acessar esta página", variant: "destructive" }); } - }, [toast]); + }, [toast, location.pathname]); // Mostra um carregamento enquanto verifica a autenticação if (isAutenticado === null) { diff --git a/src/components/layout/Sidebar.tsx b/src/components/layout/Sidebar.tsx index a02ecc1..8876016 100644 --- a/src/components/layout/Sidebar.tsx +++ b/src/components/layout/Sidebar.tsx @@ -8,7 +8,7 @@ import { ChevronLeft, ChevronRight, Home, CalendarDays, DollarSign, PieChart } f const navItems = [ { name: 'Dashboard', - path: '/', + path: '/transacoes', // Alterado de '/' para '/transacoes' icon: }, { diff --git a/src/integrations/supabase/types.ts b/src/integrations/supabase/types.ts index b26767a..2607e66 100644 --- a/src/integrations/supabase/types.ts +++ b/src/integrations/supabase/types.ts @@ -156,6 +156,50 @@ export type Database = { } Relationships: [] } + "transacoes_21f0ba76-bc06-4428-a80a-2229205fba58": { + Row: { + categoria: string | null + created_at: string + detalhes: string | null + estabelecimento: string | null + id: number + quando: string | null + tipo: string | null + usuario_id: string | null + valor: number | null + } + Insert: { + categoria?: string | null + created_at?: string + detalhes?: string | null + estabelecimento?: string | null + id?: number + quando?: string | null + tipo?: string | null + usuario_id?: string | null + valor?: number | null + } + Update: { + categoria?: string | null + created_at?: string + detalhes?: string | null + estabelecimento?: string | null + id?: number + quando?: string | null + tipo?: string | null + usuario_id?: string | null + valor?: number | null + } + Relationships: [ + { + foreignKeyName: "transacoes_21f0ba76-bc06-4428-a80a-2229205fba58_usuario_id_fkey" + columns: ["usuario_id"] + isOneToOne: false + referencedRelation: "usuarios" + referencedColumns: ["id"] + }, + ] + } usuarios: { Row: { created_at: string