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).
This commit is contained in:
22
src/components/ProtectedRoute.jsx
Normal file
22
src/components/ProtectedRoute.jsx
Normal file
@@ -0,0 +1,22 @@
|
||||
// [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;
|
||||
Reference in New Issue
Block a user