Update App.tsx

This commit is contained in:
Dosseh91
2025-06-23 20:23:20 +02:00
committed by GitHub
parent 558995b275
commit affe468a49

View File

@@ -12,29 +12,26 @@ import { AuthProvider, useAuth } from './contexts/AuthContext';
import { ListingProvider } from './contexts/ListingContext'; import { ListingProvider } from './contexts/ListingContext';
import Register from './pages/Register'; import Register from './pages/Register';
//register button route
<Route path="/register" element={<Register />} />
// Protected route component // Protected route component
const ProtectedRoute: React.FC<{ const ProtectedRoute: React.FC<{
children: React.ReactNode; children: React.ReactNode;
allowedRoles?: string[]; allowedRoles?: string[];
}> = ({ children, allowedRoles }) => { }> = ({ children, allowedRoles }) => {
const { user, loading } = useAuth(); const { user, loading } = useAuth();
const location = useLocation(); const location = useLocation();
if (loading) { if (loading) {
return <div className="min-h-screen flex items-center justify-center">Loading...</div>; return <div className="min-h-screen flex items-center justify-center">Loading...</div>;
} }
if (!user) { if (!user) {
return <Navigate to="/login" state={{ from: location }} replace />; return <Navigate to="/login" state={{ from: location }} replace />;
} }
if (allowedRoles && !allowedRoles.includes(user.role)) { if (allowedRoles && !allowedRoles.includes(user.role)) {
return <Navigate to="/" replace />; return <Navigate to="/" replace />;
} }
return <>{children}</>; return <>{children}</>;
}; };
@@ -62,26 +59,29 @@ function App() {
<Route path="/listings/:id" element={<MainLayout><ListingDetail /></MainLayout>} /> <Route path="/listings/:id" element={<MainLayout><ListingDetail /></MainLayout>} />
<Route path="/login" element={<Login />} /> <Route path="/login" element={<Login />} />
{/* ADD THIS LINE - The fix for your button */}
<Route path="/register" element={<Register />} />
{/* Protected admin routes */} {/* Protected admin routes */}
<Route <Route
path="/admin/dashboard" path="/admin/dashboard"
element={ element={
<ProtectedRoute allowedRoles={['admin']}> <ProtectedRoute allowedRoles={['admin']}>
<MainLayout><AdminDashboard /></MainLayout> <MainLayout><AdminDashboard /></MainLayout>
</ProtectedRoute> </ProtectedRoute>
} }
/> />
{/* Protected agency routes */} {/* Protected agency routes */}
<Route <Route
path="/agency/dashboard" path="/agency/dashboard"
element={ element={
<ProtectedRoute allowedRoles={['agency']}> <ProtectedRoute allowedRoles={['agency']}>
<MainLayout><AgencyDashboard /></MainLayout> <MainLayout><AgencyDashboard /></MainLayout>
</ProtectedRoute> </ProtectedRoute>
} }
/> />
{/* Redirect unknown routes to home */} {/* Redirect unknown routes to home */}
<Route path="*" element={<Navigate to="/" replace />} /> <Route path="*" element={<Navigate to="/" replace />} />
</Routes> </Routes>