Spaces:
Running
Running
Download frontend/components/ProtectedRoute.tsx from axeronis/microfunnel-api: direct link, hf CLI and curl.
- Browser
- Download file 3.81 kB
-
https://huggingface.co/spaces/axeronis/microfunnel-api/resolve/main/frontend/components/ProtectedRoute.tsx
- Command line
-
hf download hf://spaces/axeronis/microfunnel-api/frontend/components/ProtectedRoute.tsx
-
curl -L -o ProtectedRoute.tsx https://huggingface.co/spaces/axeronis/microfunnel-api/resolve/main/frontend/components/ProtectedRoute.tsx
3.81 kB
| 'use client' | |
| import { useEffect } from 'react' | |
| import { useRouter, usePathname } from 'next/navigation' | |
| import { useAuth } from '@/contexts/AuthContext' | |
| import { hasAccess } from '@/lib/permissions' | |
| import { AlertCircle, Lock } from 'lucide-react' | |
| interface ProtectedRouteProps { | |
| children: React.ReactNode | |
| requiredRole?: 'OWNER' | 'MANAGER' | |
| fallbackPath?: string | |
| } | |
| /** | |
| * Компонент для защиты маршрутов по ролям | |
| * | |
| * Использование: | |
| * <ProtectedRoute requiredRole="OWNER"> | |
| * <AdminContent /> | |
| * </ProtectedRoute> | |
| */ | |
| export function ProtectedRoute({ | |
| children, | |
| requiredRole = 'OWNER', | |
| fallbackPath = '/chats' | |
| }: ProtectedRouteProps) { | |
| const { user, isLoading } = useAuth() | |
| const router = useRouter() | |
| const pathname = usePathname() | |
| useEffect(() => { | |
| if (isLoading) return | |
| // Если пользователь не авторизован | |
| if (!user) { | |
| router.push('/login') | |
| return | |
| } | |
| // Если требуется роль OWNER, а у пользователя MANAGER | |
| if (requiredRole === 'OWNER' && user.role !== 'OWNER') { | |
| router.push(fallbackPath) | |
| return | |
| } | |
| // Проверка доступа к текущему пути | |
| if (!hasAccess(user.role as any, pathname)) { | |
| router.push(fallbackPath) | |
| } | |
| }, [user, isLoading, pathname, requiredRole, fallbackPath, router]) | |
| // Показываем загрузку | |
| if (isLoading) { | |
| return ( | |
| <div className="min-h-screen bg-gradient-to-br from-slate-50 via-indigo-50/30 to-purple-50/20 flex items-center justify-center"> | |
| <div className="text-center"> | |
| <div className="animate-spin rounded-full h-16 w-16 border-b-4 border-indigo-600 mx-auto mb-4"></div> | |
| <p className="text-lg font-semibold text-slate-700">Проверка доступа...</p> | |
| </div> | |
| </div> | |
| ) | |
| } | |
| // Если нет пользователя или нет доступа | |
| if (!user || (requiredRole === 'OWNER' && user.role !== 'OWNER')) { | |
| return ( | |
| <div className="min-h-screen bg-gradient-to-br from-slate-50 via-indigo-50/30 to-purple-50/20 flex items-center justify-center p-4"> | |
| <div className="bg-white rounded-2xl shadow-2xl border-2 border-red-200 p-8 max-w-md w-full text-center"> | |
| <div className="w-16 h-16 bg-red-100 rounded-full flex items-center justify-center mx-auto mb-4"> | |
| <Lock className="w-8 h-8 text-red-600" /> | |
| </div> | |
| <h2 className="text-2xl font-bold text-slate-900 mb-2">Доступ запрещён</h2> | |
| <p className="text-slate-600 mb-6"> | |
| У вас нет прав для просмотра этой страницы. Требуется роль: <strong>{requiredRole === 'OWNER' ? 'Владелец' : 'Менеджер'}</strong> | |
| </p> | |
| <div className="flex items-start gap-3 p-4 bg-amber-50 border-2 border-amber-200 rounded-xl mb-6"> | |
| <AlertCircle className="w-5 h-5 text-amber-600 flex-shrink-0 mt-0.5" /> | |
| <p className="text-sm text-amber-800 text-left"> | |
| Обратитесь к владельцу аккаунта для получения необходимых прав доступа. | |
| </p> | |
| </div> | |
| <button | |
| onClick={() => router.push(fallbackPath)} | |
| className="w-full px-6 py-3 bg-gradient-to-r from-indigo-600 to-purple-600 hover:from-indigo-500 hover:to-purple-500 text-white font-bold rounded-xl transition-all shadow-lg hover:shadow-xl" | |
| > | |
| Вернуться к чатам | |
| </button> | |
| </div> | |
| </div> | |
| ) | |
| } | |
| // Пользователь авторизован и имеет доступ | |
| return <>{children}</> | |
| } | |