Fix: Redirect to login on dashboard navigation

The user is redirected to the login screen when navigating to the dashboard after logging in. This commit addresses the issue.
This commit is contained in:
gpt-engineer-app[bot] 2025-05-18 13:15:39 +00:00
parent 6bed5a8fc4
commit 3028d8629d
4 changed files with 83 additions and 10 deletions

View File

@ -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<boolean | null>(null);
useEffect(() => {
// Verifica se o usuário está autenticado
const autenticado = localStorage.getItem('autenticado') === 'true';
const userId = localStorage.getItem('userId');
setIsAutenticado(autenticado && !!userId);
}, []);
return (
<React.StrictMode>
<QueryClientProvider client={queryClient}>
@ -32,11 +41,29 @@ const App = () => {
<Sonner />
<BrowserRouter>
<Routes>
{/* Rota inicial redireciona para autenticação ou dashboard */}
<Route path="/" element={<Navigate to="/auth" replace />} />
{/* Rota inicial redireciona para dashboard ou autenticação */}
<Route
path="/"
element={
isAutenticado ? (
<Navigate to="/transacoes" replace />
) : (
<Navigate to="/auth" replace />
)
}
/>
{/* Rota de autenticação */}
<Route path="/auth" element={<Auth />} />
{/* Rota de autenticação - redireciona para transações se já estiver autenticado */}
<Route
path="/auth"
element={
isAutenticado ? (
<Navigate to="/transacoes" replace />
) : (
<Auth />
)
}
/>
{/* Rotas protegidas que exigem autenticação */}
<Route path="/transacoes" element={

View File

@ -1,6 +1,6 @@
import { ReactNode, useEffect, useState } from 'react';
import { Navigate } from 'react-router-dom';
import { Navigate, useLocation } from 'react-router-dom';
import { useToast } from "@/components/ui/use-toast";
interface ProtectedRouteProps {
@ -10,20 +10,22 @@ interface ProtectedRouteProps {
const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
const [isAutenticado, setIsAutenticado] = useState<boolean | null>(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) {

View File

@ -8,7 +8,7 @@ import { ChevronLeft, ChevronRight, Home, CalendarDays, DollarSign, PieChart } f
const navItems = [
{
name: 'Dashboard',
path: '/',
path: '/transacoes', // Alterado de '/' para '/transacoes'
icon: <Home className="mr-2 h-5 w-5" />
},
{

View File

@ -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