- 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.
51 lines
1.5 KiB
TypeScript
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}</>
|
|
} |