A production-ready template for implementing Multi-Tenancy and Role-Based Access Control (RBAC) using Supabase.
This template provides a robust foundation for building multi-tenant applications with comprehensive role-based access control. It's designed to be minimal yet production-ready, allowing developers to extend it based on their specific needs.
- 🏢 Multi-Tenancy: Full isolation between different tenants
- 🔐 Role-Based Access Control: Flexible permission system
- 🔑 Row Level Security: Secure data access patterns
- 🌍 System-wide and Tenant-specific Roles: Granular access control
- 📝 Comprehensive Audit Logging: Track all system events
- ⚡ Ready-to-use: Just clone and deploy
The system implements multi-tenancy through:
- Tenant Isolation: Each tenant has its own isolated space
- Member Management: Users can belong to multiple tenants
- Role Assignment: Both system-wide and tenant-specific roles
The RBAC system consists of:
Granular actions users can perform:
- System-level
system.all
: Full system accesssystem.users.manage
: Manage system userssystem.roles.manage
: Manage system roles
- Tenant-level
tenants.create
: Create new tenantstenants.read
: View tenant detailstenants.update
: Update tenant settingstenants.delete
: Remove tenantstenants.members.assign
: Manage tenant memberstenants.roles.edit
: Modify tenant roles
Collections of permissions:
- System Roles
system_admin
: Full system accessbasic_user
: Can create tenants
- Tenant Roles
administrator
: Full tenant accessmember
: Basic tenant access
Managed through the tenant_user_roles
table:
- System-wide roles (null tenant_id)
- Tenant-specific roles
The system includes a comprehensive audit logging mechanism that tracks all important events:
- User Events
- User creation, updates, and deletion
- Authentication events (login/logout)
- Role assignments and removals
- Tenant Events
- Tenant creation, updates, and deletion
- Member additions and removals
- Role assignments within tenants
- Role Management
- Role creation and deletion
- Role permission updates
- System and tenant-level role changes
- Secure Logging: Only the system can write logs
- Rich Metadata: Each log entry includes:
- Timestamp
- Event type
- Actor (user who performed the action)
- Tenant context (if applicable)
- Detailed event description
- Additional metadata
- Access Control: Logs are accessible based on user permissions
- Querying: Supports filtering by date, event type, tenant, and actor
- Supabase CLI
- Node.js 18+ (for example implementation)
-
Clone the repository
git clone https://github.com/yourusername/supabase-multi-tenant-rbac cd supabase-multi-tenant-rbac
-
Start Supabase locally
supabase start
-
Apply migrations
supabase migration up
-
Seed the database
supabase db reset
The recommended workflow using Supabase CLI:
-
Create new migrations
supabase migration new your_migration_name
-
Test locally
supabase db reset
-
Push to production
supabase db push
// utils/supabase.ts
import { createClient } from '@supabase/supabase-js'
export const createServerClient = () => {
return createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
)
}
// Example: Check permissions
async function hasPermission(permission: string, tenantId?: number) {
const supabase = createServerClient()
const { data, error } = await supabase
.rpc('authorise', {
requested_permission: permission,
tenant_id: tenantId
})
if (error) throw error
return data
}
// Create a new tenant
const { data: tenant } = await supabase
.from('tenants')
.insert({ name: 'acme' })
.select()
.single()
// Assign a role to a user
const { data: role } = await supabase
.from('tenant_user_roles')
.insert({
tenant_id: tenant.id,
user_id: 'user-uuid',
role: 'member',
role_type: 'default'
})
Check out the /examples
directory for a full implementation using:
- Next.js 14 (App Router)
- shadcn/ui
- Tailwind CSS
- TypeScript
The example demonstrates:
- User authentication
- Tenant creation and management
- Role and permission management
- Member invitation system
- Profile management
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License - feel free to use this template in your own projects.
- Audit logging system where only system can write logs, authorised users can read logs
- Invite users to join tenants using SMTP and role assignment in the invite
- Custom roles?