Files
sdb-front/src/components/ProtectedRoute.tsx
JSC 05627c55c5
All checks were successful
Frontend CI / lint (push) Successful in 9m46s
Frontend CI / build (push) Successful in 10m7s
Refactor authentication and theme context usage
- Moved authentication hooks and context to a dedicated hooks directory.
- Updated imports in various components and pages to use the new hooks.
- Created AuthContext and ThemeContext for better state management.
- Refactored ThemeProvider to utilize the new ThemeContext.
- Cleaned up sidebar and button components for consistency and readability.
- Ensured all components are using the latest context and hooks for authentication and theme management.
2025-07-01 17:50:26 +02:00

51 lines
1.5 KiB
TypeScript

import { type ReactNode } from 'react'
import { Navigate } from 'react-router'
import { useAuth } from '@/hooks/use-auth'
interface ProtectedRouteProps {
children: ReactNode
requireAdmin?: boolean
}
export function ProtectedRoute({ children, requireAdmin = false }: ProtectedRouteProps) {
const { user, loading } = useAuth()
if (loading) {
return (
<div className="min-h-screen flex items-center justify-center">
<div className="text-center">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900 mx-auto"></div>
<p className="mt-2 text-gray-600">Loading...</p>
</div>
</div>
)
}
if (!user) {
return <Navigate to="/login" replace />
}
if (requireAdmin && user.role !== 'admin') {
return (
<div className="min-h-screen flex items-center justify-center">
<div className="text-center">
<h1 className="text-2xl font-bold text-gray-900 mb-2">Access Denied</h1>
<p className="text-gray-600">You don't have permission to access this page.</p>
</div>
</div>
)
}
if (!user.is_active) {
return (
<div className="min-h-screen flex items-center justify-center">
<div className="text-center">
<h1 className="text-2xl font-bold text-gray-900 mb-2">Account Disabled</h1>
<p className="text-gray-600">Your account has been disabled. Please contact support.</p>
</div>
</div>
)
}
return <>{children}</>
}