Cursor prompt
Implement Role-Based Access Control
RBAC system with permissions, roles, API middleware guards, and UI-level permission checks for a Next.js application.
Implement Role-Based Access Control for {{project_name}}. ## Roles: {{roles}} ## Protected Resources: {{resources}} ### Permission Model Flat permission strings: {resource}:{action} Examples: users:read, users:write, billing:manage, reports:export ### Role Definitions {{role_permissions}} ### Implementation **Permission Utilities** ```typescript hasPermission(user: User, permission: string): boolean requirePermission(permission: string): Middleware // throws 403 if missing ``` **API Middleware** - Every protected route calls requirePermission() — no exceptions - Verify JWT, extract user, check permissions array - Return 403 (forbidden) not 404 — don't hide resource existence - Log all 403s with: userId, resource, action, timestamp **React Hooks and Components** ```typescript usePermission(permission: string): boolean <PermissionGate permission="users:write"> <CreateUserButton /> </PermissionGate> ``` - HIDE (not just disable) UI elements the user can't access - Gate entire page sections, not just individual buttons **Database** - Store role on User record - Compute permissions array on login and store in JWT claims - Keep permissions in JWT to avoid DB lookup on every request - Re-issue JWT when roles change ### Output Permission type definitions, requirePermission middleware, PermissionGate component, role configuration, and JWT integration.
Fill in: {{project_name}}, {{roles}}, {{resources}}, {{role_permissions}}. In Loopy you fill these in a form and copy the finished prompt in one click.
More coding prompts
- CursorAdd File Upload with Cloud StorageMulti-file upload with type checking, size limits, virus scanning, cloud storage, and progress tracking.
- CursorAdd Redis Caching LayerCache-aside Redis caching with typed get/set helpers, TTL management, pattern-based invalidation, and monitoring.
- CursorAdd Stripe Payment IntegrationComplete Stripe checkout, webhooks, subscription management, and billing portal with idempotent webhook handling.
- LovableBooking and Scheduling SystemCalendar-based booking with availability management, confirmations, reminders, and payment collection.
- CursorBuild Admin Dashboard with CRUDData table with server-side CRUD, column sorting and filtering, bulk actions, and role-protected admin panel.
- CursorBuild Data Export FeatureExport large datasets to CSV or Excel with streaming for memory efficiency, background jobs, and signed download links.

