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:
parent
6bed5a8fc4
commit
3028d8629d
37
src/App.tsx
37
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 } from "@/components/ui/toaster";
|
||||||
import { Toaster as Sonner } from "@/components/ui/sonner";
|
import { Toaster as Sonner } from "@/components/ui/sonner";
|
||||||
import { TooltipProvider } from "@/components/ui/tooltip";
|
import { TooltipProvider } from "@/components/ui/tooltip";
|
||||||
@ -24,6 +24,15 @@ const queryClient = new QueryClient({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const App = () => {
|
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 (
|
return (
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
<QueryClientProvider client={queryClient}>
|
<QueryClientProvider client={queryClient}>
|
||||||
@ -32,11 +41,29 @@ const App = () => {
|
|||||||
<Sonner />
|
<Sonner />
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
<Routes>
|
<Routes>
|
||||||
{/* Rota inicial redireciona para autenticação ou dashboard */}
|
{/* Rota inicial redireciona para dashboard ou autenticação */}
|
||||||
<Route path="/" element={<Navigate to="/auth" replace />} />
|
<Route
|
||||||
|
path="/"
|
||||||
|
element={
|
||||||
|
isAutenticado ? (
|
||||||
|
<Navigate to="/transacoes" replace />
|
||||||
|
) : (
|
||||||
|
<Navigate to="/auth" replace />
|
||||||
|
)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
|
||||||
{/* Rota de autenticação */}
|
{/* Rota de autenticação - redireciona para transações se já estiver autenticado */}
|
||||||
<Route path="/auth" element={<Auth />} />
|
<Route
|
||||||
|
path="/auth"
|
||||||
|
element={
|
||||||
|
isAutenticado ? (
|
||||||
|
<Navigate to="/transacoes" replace />
|
||||||
|
) : (
|
||||||
|
<Auth />
|
||||||
|
)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
|
||||||
{/* Rotas protegidas que exigem autenticação */}
|
{/* Rotas protegidas que exigem autenticação */}
|
||||||
<Route path="/transacoes" element={
|
<Route path="/transacoes" element={
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
import { ReactNode, useEffect, useState } from 'react';
|
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";
|
import { useToast } from "@/components/ui/use-toast";
|
||||||
|
|
||||||
interface ProtectedRouteProps {
|
interface ProtectedRouteProps {
|
||||||
@ -10,20 +10,22 @@ interface ProtectedRouteProps {
|
|||||||
const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
|
const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
|
||||||
const [isAutenticado, setIsAutenticado] = useState<boolean | null>(null);
|
const [isAutenticado, setIsAutenticado] = useState<boolean | null>(null);
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
|
const location = useLocation();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Verifica se o usuário está autenticado
|
// Verifica se o usuário está autenticado
|
||||||
const autenticado = localStorage.getItem('autenticado') === 'true';
|
const autenticado = localStorage.getItem('autenticado') === 'true';
|
||||||
setIsAutenticado(autenticado);
|
const userId = localStorage.getItem('userId');
|
||||||
|
setIsAutenticado(autenticado && !!userId);
|
||||||
|
|
||||||
if (!autenticado) {
|
if (!autenticado || !userId) {
|
||||||
toast({
|
toast({
|
||||||
title: "Acesso restrito",
|
title: "Acesso restrito",
|
||||||
description: "Faça login para acessar esta página",
|
description: "Faça login para acessar esta página",
|
||||||
variant: "destructive"
|
variant: "destructive"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [toast]);
|
}, [toast, location.pathname]);
|
||||||
|
|
||||||
// Mostra um carregamento enquanto verifica a autenticação
|
// Mostra um carregamento enquanto verifica a autenticação
|
||||||
if (isAutenticado === null) {
|
if (isAutenticado === null) {
|
||||||
|
|||||||
@ -8,7 +8,7 @@ import { ChevronLeft, ChevronRight, Home, CalendarDays, DollarSign, PieChart } f
|
|||||||
const navItems = [
|
const navItems = [
|
||||||
{
|
{
|
||||||
name: 'Dashboard',
|
name: 'Dashboard',
|
||||||
path: '/',
|
path: '/transacoes', // Alterado de '/' para '/transacoes'
|
||||||
icon: <Home className="mr-2 h-5 w-5" />
|
icon: <Home className="mr-2 h-5 w-5" />
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@ -156,6 +156,50 @@ export type Database = {
|
|||||||
}
|
}
|
||||||
Relationships: []
|
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: {
|
usuarios: {
|
||||||
Row: {
|
Row: {
|
||||||
created_at: string
|
created_at: string
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user