Spaces:
Sleeping
Sleeping
Download src/components/ProtectedRoute.jsx from Sebebeb/SecureChat: direct link, hf CLI and curl.
- Browser
- Download file 1.07 kB
-
https://huggingface.co/spaces/Sebebeb/SecureChat/resolve/main/src/components/ProtectedRoute.jsx
- Command line
-
hf download hf://spaces/Sebebeb/SecureChat/src/components/ProtectedRoute.jsx
-
curl -L -o ProtectedRoute.jsx https://huggingface.co/spaces/Sebebeb/SecureChat/resolve/main/src/components/ProtectedRoute.jsx
1.07 kB
| import { useEffect } from 'react'; | |
| import { Outlet } from 'react-router-dom'; | |
| import { useAuth } from '@/lib/AuthContext'; | |
| import UserNotRegisteredError from '@/components/UserNotRegisteredError'; | |
| const DefaultFallback = () => ( | |
| <div className="fixed inset-0 flex items-center justify-center"> | |
| <div className="w-8 h-8 border-4 border-slate-200 border-t-slate-800 rounded-full animate-spin"></div> | |
| </div> | |
| ); | |
| export default function ProtectedRoute({ fallback = <DefaultFallback />, unauthenticatedElement }) { | |
| const { isAuthenticated, isLoadingAuth, authChecked, authError, checkUserAuth } = useAuth(); | |
| useEffect(() => { | |
| if (!authChecked && !isLoadingAuth) { | |
| checkUserAuth(); | |
| } | |
| }, [authChecked, isLoadingAuth, checkUserAuth]); | |
| if (isLoadingAuth || !authChecked) { | |
| return fallback; | |
| } | |
| if (authError) { | |
| if (authError.type === 'user_not_registered') { | |
| return <UserNotRegisteredError />; | |
| } | |
| return unauthenticatedElement; | |
| } | |
| if (!isAuthenticated) { | |
| return unauthenticatedElement; | |
| } | |
| return <Outlet />; | |
| } | |