mirror of
https://github.com/clucraft/PriceGhost.git
synced 2026-05-09 15:52:41 +02:00
107 lines
2.2 KiB
TypeScript
107 lines
2.2 KiB
TypeScript
|
|
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
|
||
|
|
import { AuthProvider, useAuth } from './context/AuthContext';
|
||
|
|
import Login from './pages/Login';
|
||
|
|
import Register from './pages/Register';
|
||
|
|
import Dashboard from './pages/Dashboard';
|
||
|
|
import ProductDetail from './pages/ProductDetail';
|
||
|
|
|
||
|
|
function ProtectedRoute({ children }: { children: React.ReactNode }) {
|
||
|
|
const { user, isLoading } = useAuth();
|
||
|
|
|
||
|
|
if (isLoading) {
|
||
|
|
return (
|
||
|
|
<div
|
||
|
|
style={{
|
||
|
|
display: 'flex',
|
||
|
|
justifyContent: 'center',
|
||
|
|
alignItems: 'center',
|
||
|
|
height: '100vh',
|
||
|
|
}}
|
||
|
|
>
|
||
|
|
<span className="spinner" style={{ width: '3rem', height: '3rem' }} />
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!user) {
|
||
|
|
return <Navigate to="/login" replace />;
|
||
|
|
}
|
||
|
|
|
||
|
|
return <>{children}</>;
|
||
|
|
}
|
||
|
|
|
||
|
|
function PublicRoute({ children }: { children: React.ReactNode }) {
|
||
|
|
const { user, isLoading } = useAuth();
|
||
|
|
|
||
|
|
if (isLoading) {
|
||
|
|
return (
|
||
|
|
<div
|
||
|
|
style={{
|
||
|
|
display: 'flex',
|
||
|
|
justifyContent: 'center',
|
||
|
|
alignItems: 'center',
|
||
|
|
height: '100vh',
|
||
|
|
}}
|
||
|
|
>
|
||
|
|
<span className="spinner" style={{ width: '3rem', height: '3rem' }} />
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (user) {
|
||
|
|
return <Navigate to="/" replace />;
|
||
|
|
}
|
||
|
|
|
||
|
|
return <>{children}</>;
|
||
|
|
}
|
||
|
|
|
||
|
|
function AppRoutes() {
|
||
|
|
return (
|
||
|
|
<Routes>
|
||
|
|
<Route
|
||
|
|
path="/login"
|
||
|
|
element={
|
||
|
|
<PublicRoute>
|
||
|
|
<Login />
|
||
|
|
</PublicRoute>
|
||
|
|
}
|
||
|
|
/>
|
||
|
|
<Route
|
||
|
|
path="/register"
|
||
|
|
element={
|
||
|
|
<PublicRoute>
|
||
|
|
<Register />
|
||
|
|
</PublicRoute>
|
||
|
|
}
|
||
|
|
/>
|
||
|
|
<Route
|
||
|
|
path="/"
|
||
|
|
element={
|
||
|
|
<ProtectedRoute>
|
||
|
|
<Dashboard />
|
||
|
|
</ProtectedRoute>
|
||
|
|
}
|
||
|
|
/>
|
||
|
|
<Route
|
||
|
|
path="/product/:id"
|
||
|
|
element={
|
||
|
|
<ProtectedRoute>
|
||
|
|
<ProductDetail />
|
||
|
|
</ProtectedRoute>
|
||
|
|
}
|
||
|
|
/>
|
||
|
|
<Route path="*" element={<Navigate to="/" replace />} />
|
||
|
|
</Routes>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
export default function App() {
|
||
|
|
return (
|
||
|
|
<BrowserRouter>
|
||
|
|
<AuthProvider>
|
||
|
|
<AppRoutes />
|
||
|
|
</AuthProvider>
|
||
|
|
</BrowserRouter>
|
||
|
|
);
|
||
|
|
}
|