-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathApp.tsx
More file actions
60 lines (52 loc) · 1.74 KB
/
Copy pathApp.tsx
File metadata and controls
60 lines (52 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { useState, useEffect } from "react";
import { Navigate, Route, Routes } from "react-router-dom";
import LandingPage from "./pages/dashboard/LandingPage";
import Dashboard from "./pages/dashboard/Dashboard";
import NotFound from "./not_found";
import Loading from "./Loading";
import LoginPage from "./pages/login/LoginPage";
import RegisterPage from "./pages/register/RegisterPage";
import AuthCallback from "./pages/auth/AuthCallback";
import { useAuth } from "@/context/AuthContext";
function ProtectedRoute({ children }: { children: React.ReactNode }) {
const { user, isLoading } = useAuth();
if (isLoading) return <Loading />;
if (!user) return <Navigate to="/login" replace />;
return <>{children}</>;
}
function PublicRoute({ children }: { children: React.ReactNode }) {
const { user, isLoading } = useAuth();
if (isLoading) return <Loading />;
if (user) return <Navigate to="/app" replace />;
return <>{children}</>;
}
function App() {
const [appCarregando, setAppCarregando] = useState(true);
useEffect(() => {
const timer = setTimeout(() => {
setAppCarregando(false);
}, 2000);
return () => clearTimeout(timer);
}, []);
if (appCarregando) {
return <Loading />;
}
return (
<Routes>
<Route path="/" element={<LandingPage />} />
<Route
path="/app"
element={
<ProtectedRoute>
<Dashboard />
</ProtectedRoute>
}
/>
<Route path="/login" element={<PublicRoute><LoginPage /></PublicRoute>} />
<Route path="/register" element={<PublicRoute><RegisterPage /></PublicRoute>} />
<Route path="/auth/callback" element={<AuthCallback />} />
<Route path="*" element={<NotFound />} />
</Routes>
);
}
export default App;