Back to writes

Designing Multi-Role RBAC in Next.js and Postgres

1 min readarchitecture, nextjs, postgres

Practical patterns for separating tenant membership, role permissions, and route guards when building B2B SaaS dashboards.

Early in a SaaS build, checking user.role === "admin" feels harmless. Once you introduce organizations, instructors, operators, and billing owners, hardcoded role strings leak across every route handler.

Check capabilities, not role names

Mapping each tenant role to explicit capabilities in one place keeps API handlers readable and prevents authorization drift between UI and backend.

lib/auth/rbac.ts
export type Role = 'owner' | 'operator' | 'viewer'

const PERMISSIONS: Record<Role, readonly string[]> = {
  owner: ['billing:write', 'catalog:write', 'orders:read'],
  operator: ['catalog:write', 'orders:read'],
  viewer: ['orders:read'],
}

export function can(role: Role, permission: string): boolean {
  return PERMISSIONS[role]?.includes(permission) ?? false
}

Scope every query by tenant

Pair capability checks with explicit tenant_id predicates or database row-level security so cross-tenant reads are impossible even if a handler misses a check.

Thanks for reading.

Md Afzal Mir

Home
action
Work
action
Writes
action
About
action
Uses
action
Bookshelf
action
CAART
Multi-tenant WhatsApp-first SaaS catalog and storefront engine enabling local businesses to launch online stores and convert orders directly.
project
AI Smart Class
Multi-role EdTech LMS with RBAC, adaptive video streaming via Bunny.net, Excalidraw whiteboard, and automated MCQ assessments.
project
Alumni Portal
Community directory and event registration platform automating attendee onboarding and instant ID card generation.
project
Wallet X
Digital wallet fintech demo with Zod schema validation, real-time balance updates, and automated receipt generation.
project
Designing Multi-Role RBAC in Next.js and Postgres
Practical patterns for separating tenant membership, role permissions, and route guards when building B2B SaaS dashboards.
write
Serving Course Assets Safely with Cloudflare R2 Signed URLs
How to keep PDFs and study assets private in object storage while streaming them quickly to enrolled students.
write
Arrow keys to navigate⌘K