import React from 'react'; import { Navigate, useLocation } from 'react-router-dom'; import { useAuth } from '../contexts/AuthContext'; /** * ProtectedRoute: checks session via AuthContext. * allowedRoles: array of roles that can access this route. * If no session, redirects to /login. * If role set but not allowed, redirects to /. */ export default function ProtectedRoute({ children, allowedRoles }) { const { session, role, loading } = useAuth(); const location = useLocation(); if (loading) { return
Authentifizierung läuft...
; } if (!session) { return ; } if (allowedRoles && !allowedRoles.includes(role)) { return ; } return children; }