Implement Clerk multi-tenant organization features with RBAC, role-based access control, organization switching, member management, and tenant isolation. Use when building multi-tenant SaaS applications, implementing organization hierarchies, configuring custom roles and permissions, setting up organization-scoped data isolation, or when user mentions organizations, RBAC, multi-tenancy, roles, permissions, organization switcher, member management, or tenant isolation.
Limited to specific tools
Additional assets for this skill
This skill is limited to using the following tools:
README.mdexamples/multi-tenant-app.tsxexamples/org-admin-dashboard.tsxscripts/configure-roles.shscripts/setup-organizations.shscripts/test-org-isolation.shtemplates/organization-schema.mdtemplates/organization-switcher.tsxtemplates/rbac-policies.tsPurpose: Autonomously implement and configure Clerk organization features for multi-tenant applications with RBAC.
Activation Triggers:
Key Resources:
scripts/setup-organizations.sh - Enable and configure organizationsscripts/configure-roles.sh - Setup RBAC with custom rolesscripts/test-org-isolation.sh - Test tenant data isolationtemplates/organization-schema.md - Multi-tenant database schema patternstemplates/rbac-policies.ts - Role and permission definitionstemplates/organization-switcher.tsx - Organization switcher componentexamples/multi-tenant-app.tsx - Complete multi-tenant applicationexamples/org-admin-dashboard.tsx - Organization admin interfaceBefore implementing organization features, enable them in your Clerk Dashboard:
# Run setup script to guide through Clerk Dashboard configuration
./scripts/setup-organizations.sh
Manual steps (documented in script):
# Setup custom roles and permissions
./scripts/configure-roles.sh [basic|advanced|custom]
# Examples:
./scripts/configure-roles.sh basic # Admin + Member only
./scripts/configure-roles.sh advanced # Admin + Manager + Member + Viewer
./scripts/configure-roles.sh custom # Interactive custom role creation
Outputs:
Use templates to create organization UI:
Organization Switcher (for multi-org users):
# Copy and customize organization switcher
cp templates/organization-switcher.tsx src/components/OrganizationSwitcher.tsx
Organization Schema (for database integration):
# View multi-tenant database schema patterns
cat templates/organization-schema.md
RBAC Policies:
# Copy RBAC policy definitions
cp templates/rbac-policies.ts src/lib/rbac.ts
# Test tenant data isolation (requires project running)
./scripts/test-org-isolation.sh
# Validates:
# - Data scoped to organization_id
# - Cross-tenant data leakage prevention
# - Role-based access enforcement
# - Organization switching works correctly
Row-Level Security (RLS) Pattern:
-- Every table includes organization_id
CREATE TABLE projects (
id UUID PRIMARY KEY,
organization_id TEXT NOT NULL,
name TEXT NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
-- RLS Policy (Supabase/Postgres)
CREATE POLICY "Users can only access their org's projects"
ON projects FOR ALL
USING (organization_id = current_setting('app.current_organization_id'));
Application-Level Scoping:
// Always filter by organization_id in queries
const projects = await db.projects.findMany({
where: {
organizationId: user.organizationId
}
});
Level 1: Basic (Admin/Member)
Level 2: Advanced (4+ roles)
Level 3: Custom (Granular permissions)
project:create, billing:manage, members:inviteSee templates/organization-switcher.tsx for complete component with:
See examples/org-admin-dashboard.tsx for:
See examples/multi-tenant-app.tsx for:
# Enable organizations + advanced RBAC
./scripts/setup-organizations.sh
./scripts/configure-roles.sh advanced
# Implement organization switcher
cp templates/organization-switcher.tsx src/components/
# Enable organizations + custom RBAC with granular permissions
./scripts/setup-organizations.sh
./scripts/configure-roles.sh custom
# Setup database isolation
# Follow templates/organization-schema.md for RLS setup
# Test isolation after implementing schema
./scripts/test-org-isolation.sh
# Validates:
# - No cross-tenant data access
# - RLS policies working correctly
# - Organization switching updates context
Middleware Protection:
// Check role in middleware
import { auth } from '@clerk/nextjs/server';
export default async function middleware(req: Request) {
const { orgRole } = await auth();
if (orgRole !== 'org:admin') {
return new Response('Forbidden', { status: 403 });
}
}
Component-Level Checks:
// Hide UI based on role
import { useOrganization } from '@clerk/nextjs';
function AdminOnlyButton() {
const { membership } = useOrganization();
if (membership?.role !== 'org:admin') return null;
return <button>Admin Action</button>;
}
Custom Permission Checks:
// Check specific permission
import { checkPermission } from '@/lib/rbac';
if (await checkPermission(user, 'billing:manage')) {
// Allow billing access
}
See templates/organization-schema.md for complete schema patterns.
// Organization-scoped queries
const projects = await prisma.project.findMany({
where: {
organizationId: user.organizationId
}
});
// Middleware to auto-inject organization_id
prisma.$use(async (params, next) => {
if (params.action === 'create') {
params.args.data.organizationId = getCurrentOrgId();
}
return next(params);
});
# .env.example (no real keys!)
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_your_clerk_key_here
CLERK_SECRET_KEY=sk_test_your_clerk_secret_here
# Organization feature flags (optional)
NEXT_PUBLIC_ENABLE_ORG_CREATION=true
NEXT_PUBLIC_MAX_ORGS_PER_USER=5
Organizations not appearing:
RBAC not working:
Data isolation failing:
Scripts: All scripts in scripts/ directory are executable and include detailed usage instructions
Templates: templates/ contains production-ready components and schema patterns
Examples: examples/ contains complete application examples with organization features
Clerk Dashboard Configuration Required: Organizations must be enabled in your Clerk Dashboard before using this skill
Framework Support: Next.js (App Router & Pages Router), React, Remix, Gatsby
Version: 1.0.0 Clerk SDK Compatibility: @clerk/nextjs 5+, @clerk/clerk-react 5+