Spaces:
Paused
Paused
Download src/components/ProtectedRoute.tsx from shanshal66/chinak2: direct link, hf CLI and curl.
- Browser
- Download file 1.14 kB
-
https://huggingface.co/spaces/shanshal66/chinak2/resolve/main/src/components/ProtectedRoute.tsx
- Command line
-
hf download hf://spaces/shanshal66/chinak2/src/components/ProtectedRoute.tsx
-
curl -L -o ProtectedRoute.tsx https://huggingface.co/spaces/shanshal66/chinak2/resolve/main/src/components/ProtectedRoute.tsx
1.14 kB
| import React from 'react'; | |
| import { Navigate, useLocation } from 'react-router-dom'; | |
| import { useAuthStore } from '../store/useAuthStore'; | |
| import { useShallow } from 'zustand/react/shallow'; | |
| interface ProtectedRouteProps { | |
| children: React.ReactNode; | |
| requireAdmin?: boolean; | |
| } | |
| const ProtectedRoute: React.FC<ProtectedRouteProps> = ({ children, requireAdmin = false }) => { | |
| const location = useLocation(); | |
| const { isAuthenticated, user, isLoading } = useAuthStore( | |
| useShallow((state) => ({ | |
| isAuthenticated: state.isAuthenticated, | |
| user: state.user, | |
| isLoading: state.isLoading | |
| })) | |
| ); | |
| if (isLoading) { | |
| return ( | |
| <div className="min-h-screen flex items-center justify-center bg-background-light dark:bg-background-dark"> | |
| <div className="animate-spin rounded-full h-12 w-12 border-b-2 border-primary"></div> | |
| </div> | |
| ); | |
| } | |
| if (!isAuthenticated) { | |
| return <Navigate to="/login" state={{ from: location }} replace />; | |
| } | |
| if (requireAdmin && user?.role !== 'ADMIN') { | |
| return <Navigate to="/" replace />; | |
| } | |
| return <>{children}</>; | |
| }; | |
| export default ProtectedRoute; | |