Spaces:
Running
Running
Download client/src/components/PrivateRoute.jsx from mb672038/python-mastery-academy: direct link, hf CLI and curl.
- Browser
- Download file 611 Bytes
-
https://huggingface.co/spaces/mb672038/python-mastery-academy/resolve/main/client/src/components/PrivateRoute.jsx
- Command line
-
hf download hf://spaces/mb672038/python-mastery-academy/client/src/components/PrivateRoute.jsx
-
curl -L -o PrivateRoute.jsx https://huggingface.co/spaces/mb672038/python-mastery-academy/resolve/main/client/src/components/PrivateRoute.jsx
611 Bytes
| import { useAuth } from '../context/AuthContext'; | |
| import { Navigate, Outlet } from 'react-router-dom'; | |
| import { toast } from 'react-toastify'; | |
| export default function PrivateRoute({ roles, children }) { | |
| const { user, isAuthenticated, hasRole } = useAuth(); | |
| if (!isAuthenticated()) { | |
| toast.error('لطفا ابتدا وارد شوید'); | |
| return <Navigate to="/login" />; | |
| } | |
| if (roles && !roles.some(role => hasRole(role))) { | |
| toast.error('شما مجوز دسترسی به این صفحه را ندارید'); | |
| return <Navigate to="/" />; | |
| } | |
| return children ? children : <Outlet />; | |
| } |