cre-sync/components/auth/RoleGate.tsx
BusyBee3333 4e6467ffb0 Add CRESync CRM application with Setup page
- Build complete Next.js CRM for commercial real estate
- Add authentication with JWT sessions and role-based access
- Add GoHighLevel API integration for contacts, conversations, opportunities
- Add AI-powered Control Center with tool calling
- Add Setup page with onboarding checklist (/setup)
- Add sidebar navigation with Setup menu item
- Fix type errors in onboarding API, GHL services, and control center tools
- Add Prisma schema with SQLite for local development
- Add UI components with clay morphism design system

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-14 17:30:55 -05:00

53 lines
1.5 KiB
TypeScript

'use client';
import { useAuth } from '@/lib/hooks/useAuth';
import { Permission, hasPermission, hasAnyPermission, isAdmin, isSuperAdmin } from '@/lib/auth/roles';
import { Role } from '@/types/auth';
interface RoleGateProps {
children: React.ReactNode;
requiredPermissions?: Permission[];
anyPermission?: Permission[];
requireAdmin?: boolean;
requireSuperAdmin?: boolean;
fallback?: React.ReactNode;
}
export function RoleGate({
children,
requiredPermissions,
anyPermission,
requireAdmin,
requireSuperAdmin,
fallback = null,
}: RoleGateProps) {
const { user } = useAuth();
if (!user) return <>{fallback}</>;
const userRole = user.role as Role;
if (requireSuperAdmin && !isSuperAdmin(userRole)) return <>{fallback}</>;
if (requireAdmin && !isAdmin(userRole)) return <>{fallback}</>;
if (requiredPermissions?.length) {
const hasAll = requiredPermissions.every(p => hasPermission(userRole, p));
if (!hasAll) return <>{fallback}</>;
}
if (anyPermission?.length) {
if (!hasAnyPermission(userRole, anyPermission)) return <>{fallback}</>;
}
return <>{children}</>;
}
// Convenience components
export function AdminOnly({ children, fallback }: { children: React.ReactNode; fallback?: React.ReactNode }) {
return <RoleGate requireAdmin fallback={fallback}>{children}</RoleGate>;
}
export function SuperAdminOnly({ children, fallback }: { children: React.ReactNode; fallback?: React.ReactNode }) {
return <RoleGate requireSuperAdmin fallback={fallback}>{children}</RoleGate>;
}