Authentication
Hiro CRM uses Supabase Authentication for securing API endpoints. Authentication is managed through session cookies and JWT tokens.Authentication Methods
1. Session-Based (Recommended for Web)
API routes automatically access the user session via cookies usingcreateClient() from @supabase/ssr.
2. Service Role Key (Admin Operations)
For admin endpoints that bypass Row-Level Security (RLS), use the service role key:3. Cron Secret (Scheduled Jobs)
Cron endpoints require aCRON_SECRET in the Authorization header:
Permission System
Hiro uses role-based permissions defined in/lib/authz.ts:
User Roles
| Role | Description |
|---|---|
super_admin | Full platform access |
admin | Organization-wide access |
manager | Location management access |
marketing | Marketing and customer operations |
operaciones | Operations and reservations |
viewer | Read-only access |
Permission Helpers
requireAuth()
Verify authentication and retrieve user profile:
supabase: Authenticated Supabase clientuser: Supabase user objectrole: User’s role (UserRole type)isPlatformAdmin: Boolean (true for super_admin)
requirePermission()
Verify specific permission before allowing access:
canViewSettings- Access settings pagescanAccessOperaciones- Access operations/reservationscanManageAutomations- Manage marketing automationscanManageCustomers- Manage customer datacanManageLocations- Manage locationscanManageUsers- Manage users and roles
Example: Authenticated Endpoint
app/api/example/route.ts
Error Responses
401 Unauthorized
403 Forbidden
400 Bad Request
Security Best Practices
Always validate user authentication before processing requests
Scope data to user’s organization using
organization_id filtersUse
requirePermission() for sensitive operationsNever expose service role keys in client-side code
Validate CRON_SECRET for all scheduled job endpoints
