Barazo default frontend barazo.forum
at main 48 lines 1.4 kB view raw
1/** 2 * Admin route layout. 3 * Gates all /admin/* pages on authentication + admin role. 4 * Shows loading skeleton while auth state initializes, 5 * redirects non-admin users to the homepage. 6 * @see specs/prd-web.md Section M11 7 */ 8 9'use client' 10 11import { useEffect } from 'react' 12import { useRouter } from 'next/navigation' 13import { useAuth } from '@/hooks/use-auth' 14 15export default function AdminLayout({ children }: { children: React.ReactNode }) { 16 const { user, isAuthenticated, isLoading } = useAuth() 17 const router = useRouter() 18 19 useEffect(() => { 20 if (!isLoading && !isAuthenticated) { 21 router.replace(`/login?returnTo=${encodeURIComponent('/admin')}`) 22 } 23 }, [isLoading, isAuthenticated, router]) 24 25 useEffect(() => { 26 if (!isLoading && isAuthenticated && user?.role !== 'admin') { 27 router.replace('/') 28 } 29 }, [isLoading, isAuthenticated, user?.role, router]) 30 31 if (isLoading) { 32 return ( 33 <div className="animate-pulse space-y-4 p-6" role="status" aria-label="Loading admin panel"> 34 <div className="h-8 w-48 rounded bg-muted" /> 35 <div className="h-4 w-full rounded bg-muted" /> 36 <div className="h-4 w-3/4 rounded bg-muted" /> 37 <div className="h-32 rounded bg-muted" /> 38 <span className="sr-only">Loading admin panel</span> 39 </div> 40 ) 41 } 42 43 if (!isAuthenticated || user?.role !== 'admin') { 44 return null 45 } 46 47 return <>{children}</> 48}