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