diff --git a/src/App.tsx b/src/App.tsx index 7cb188f..cf30afc 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -45,8 +45,16 @@ function App() { } /> } /> } /> - } /> + + + + + } + /> } /> diff --git a/src/assets/components/ProtectedRoute.tsx b/src/assets/components/ProtectedRoute.tsx index da2116c..723eee1 100644 --- a/src/assets/components/ProtectedRoute.tsx +++ b/src/assets/components/ProtectedRoute.tsx @@ -1,15 +1,38 @@ import { Navigate } from 'react-router-dom'; import type { ReactNode } from 'react'; import { useAuth } from '@/contexts/AuthContext'; +import NotFound from '@/pages/404/NotFoundPage'; interface ProtectedRouteProps { children: ReactNode; + requiredRole?: string | string[]; } -export default function ProtectedRoute({ children }: ProtectedRouteProps) { - const { isAuthenticated } = useAuth(); +export default function ProtectedRoute({ + children, + requiredRole, +}: ProtectedRouteProps) { + const { isAuthenticated, user } = useAuth(); if (!isAuthenticated) { return ; } + + if (requiredRole) { + const userRoles: string[] = Array.isArray(user?.role) + ? (user?.role as string[]) + : user?.role + ? [user.role as string] + : []; + + const required = Array.isArray(requiredRole) + ? requiredRole + : [requiredRole]; + + const hasRole = required.some((r) => userRoles.includes(r)); + if (!hasRole) { + return ; + } + } + return <>{children}; } diff --git a/vercel.json b/vercel.json index 0d199b2..0f32683 100644 --- a/vercel.json +++ b/vercel.json @@ -1,5 +1,3 @@ { - "rewrites": [ - { "source": "/(.*)", "destination": "/index.html" } - ] + "rewrites": [{ "source": "/(.*)", "destination": "/index.html" }] }