Files
BIU_System_Rezerwacji/src/components/ProtectedRoute.jsx
Krzysztof Cieślik f436d87ca5 Inicjalna wersja systemu zarządzania rezerwacjami (RMS)
SPA zbudowane w React 19 + Vite 8 z pełnym zestawem funkcjonalności:
autentykacja z 2FA, kreator rezerwacji, panel admina, analityka,
GraphQL (Apollo Client + SchemaLink), React Query, Storybook,
testy jednostkowe (Vitest + RTL) i e2e (Playwright).
2026-06-21 06:08:47 +02:00

23 lines
749 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// [REQ F1] System uwierzytelniania ochrona tras, przekierowanie niezalogowanych do /login
// [REQ T3] React Router Outlet + Navigate, zachowanie ścieżki powrotu w location.state
import { Navigate, Outlet, useLocation } from 'react-router-dom';
import { useAuth } from '../context/AuthContext';
const ProtectedRoute = ({ allowedRoles }) => {
const { user } = useAuth();
const location = useLocation();
if (!user) {
return <Navigate to="/login" state={{ from: location }} replace />;
}
if (allowedRoles && !allowedRoles.includes(user.role)) {
const fallback = user.role === 'admin' ? '/admin' : '/dashboard';
return <Navigate to={fallback} replace />;
}
return <Outlet />;
};
export default ProtectedRoute;