Fix: App not rendering any pages
The app was not rendering any pages.
This commit is contained in:
parent
26cd75ae81
commit
0dc7667f05
@ -1,5 +1,5 @@
|
|||||||
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
|
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
|
||||||
import { Suspense, lazy, useEffect } from 'react';
|
import { Suspense, lazy } from 'react';
|
||||||
import { Toaster } from "@/components/ui/sonner";
|
import { Toaster } from "@/components/ui/sonner";
|
||||||
import Auth from './pages/Auth';
|
import Auth from './pages/Auth';
|
||||||
import ProtectedRoute from './components/auth/ProtectedRoute';
|
import ProtectedRoute from './components/auth/ProtectedRoute';
|
||||||
@ -19,14 +19,7 @@ const Configuracoes = lazy(() => import('./pages/Configuracoes'));
|
|||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const isLoggedIn = authStore((state) => state.isLoggedIn);
|
const isLoggedIn = authStore((state) => state.isLoggedIn);
|
||||||
const setLoggedIn = authStore((state) => state.setLoggedIn);
|
|
||||||
|
|
||||||
// Initialize authStore state once at mount time
|
|
||||||
useEffect(() => {
|
|
||||||
const storedAuth = localStorage.getItem('autenticado') === 'true';
|
|
||||||
setLoggedIn(storedAuth);
|
|
||||||
}, [setLoggedIn]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Router>
|
<Router>
|
||||||
<Toaster />
|
<Toaster />
|
||||||
|
|||||||
@ -11,20 +11,18 @@ interface ProtectedRouteProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
|
const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
|
||||||
|
// We use local state here to avoid re-renders of the whole app
|
||||||
const [session, setSession] = useState<Session | null>(null);
|
const [session, setSession] = useState<Session | null>(null);
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
|
|
||||||
|
// We still update the global store for other components to use
|
||||||
const setLoggedIn = authStore((state) => state.setLoggedIn);
|
const setLoggedIn = authStore((state) => state.setLoggedIn);
|
||||||
const setUser = authStore((state) => state.setUser);
|
const setUser = authStore((state) => state.setUser);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Checa a sessão ao carregar o componente
|
// onAuthStateChange fires once on initial load with the current session,
|
||||||
supabase.auth.getSession().then(({ data: { session } }) => {
|
// and then every time the auth state changes.
|
||||||
setSession(session);
|
|
||||||
setIsLoading(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Escuta por mudanças no estado de autenticação (login/logout)
|
|
||||||
const { data: { subscription } } = supabase.auth.onAuthStateChange(
|
const { data: { subscription } } = supabase.auth.onAuthStateChange(
|
||||||
(_event, session) => {
|
(_event, session) => {
|
||||||
setSession(session);
|
setSession(session);
|
||||||
@ -48,11 +46,11 @@ const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
// Redireciona para a página de login se não houver sessão
|
// Redirect to the login page if there is no session
|
||||||
return <Navigate to="/auth" state={{ from: location }} replace />;
|
return <Navigate to="/auth" state={{ from: location }} replace />;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Renderiza o conteúdo protegido se houver sessão
|
// Render the protected content if a session exists
|
||||||
return <>{children}</>;
|
return <>{children}</>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
@ -20,15 +19,6 @@ const Header = () => {
|
|||||||
const [userName, setUserName] = useState('Usuário');
|
const [userName, setUserName] = useState('Usuário');
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchUser = async () => {
|
|
||||||
const { data: { user } } = await supabase.auth.getUser();
|
|
||||||
if (user) {
|
|
||||||
setUserName(user.email || 'Usuário');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
fetchUser();
|
|
||||||
|
|
||||||
const { data: { subscription } } = supabase.auth.onAuthStateChange((_event, session) => {
|
const { data: { subscription } } = supabase.auth.onAuthStateChange((_event, session) => {
|
||||||
setUserName(session?.user?.email || 'Usuário');
|
setUserName(session?.user?.email || 'Usuário');
|
||||||
});
|
});
|
||||||
|
|||||||
@ -8,27 +8,9 @@ interface AuthState {
|
|||||||
setUser: (user: { id: string } | null) => void;
|
setUser: (user: { id: string } | null) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Função para recuperar o userId inicial (se existir)
|
|
||||||
const getInitialUser = (): { id: string } | null => {
|
|
||||||
try {
|
|
||||||
const id = localStorage.getItem('userId');
|
|
||||||
return id ? { id } : null;
|
|
||||||
} catch (e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const getInitialState = (): boolean => {
|
|
||||||
try {
|
|
||||||
return localStorage.getItem('autenticado') === 'true';
|
|
||||||
} catch (e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const authStore = create<AuthState>((set) => ({
|
export const authStore = create<AuthState>((set) => ({
|
||||||
isLoggedIn: getInitialState(),
|
isLoggedIn: false,
|
||||||
user: getInitialUser(),
|
user: null,
|
||||||
setLoggedIn: (status) => set({ isLoggedIn: status }),
|
setLoggedIn: (status) => set({ isLoggedIn: status }),
|
||||||
setUser: (user) => set({ user }),
|
setUser: (user) => set({ user }),
|
||||||
}));
|
}));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user