Multi-tenancy
The tenant is part of the security context, not a filter you remember to add.
CurrentUser carries organizationId and roles, the policy layer turns them
into a Prisma where, and every scoped query applies it on the server.
What the runtime provides
Section titled “What the runtime provides”CurrentUser.organizationIdandroles(both optional).PolicyContextwithuser,organizationId, androles.scopeper resource, applied to list, count, detail, update, delete, and relation managers.- Fail closed: a resource without a policy, without an entry in
policies.ts, or whosescopereturnsnullis denied. OrganizationSwitcherin the sidebar for admins.
Tenancy is opt-in per resource: the runtime cannot guess which rows belong to a
tenant. A resource is isolated when its policy returns a tenant where.
Scaffold a multi-tenant app
Section titled “Scaffold a multi-tenant app”pnpm create dashild@latest my-app --template saasThe saas starter adds organizations, members, and projects, and enables
tenancy for you. If you want the same wiring with another starter:
npx create-dashild@latest my-app --tenancy--tenancy requires better-auth and a schema that does not have the auth
models yet (a fresh scaffold). On an existing app:
pnpm dashild init --auth better-auth --tenancy --yesinit refuses to guess when auth models already exist: add the organization
fields yourself and keep the generated lib/organization.ts and
dashild/organization-actions.ts.
What it writes
Section titled “What it writes”Organization model + User.organizationId/role in prisma/schema.prismasrc/lib/organization.ts # organizations the user sees, active onesrc/lib/session.ts # org-aware getCurrentUsersrc/dashild/organization-actions.ts # switchOrganization server actionsrc/app/<route>/[[...dashild]]/page.tsx # passes organizations + switchOrganizationThe active organization is stored in the dashild_organization cookie. It is
httpOnly, validated on every request, and an admin can only switch to an
organization that exists; a member stays in their own.
Scope every resource
Section titled “Scope every resource”import { definePolicies, hasRole, type CurrentUser } from "@dashild/core";
export const policies = definePolicies<CurrentUser>({ resources: { Project: { scope: (ctx) => ctx.organizationId ? { organizationId: ctx.organizationId } : null, can: { create: (ctx) => hasRole(ctx, "ADMIN", "USER"), update: (ctx) => hasRole(ctx, "ADMIN", "USER"), delete: (ctx) => hasRole(ctx, "ADMIN"), }, }, Organization: { scope: (ctx) => (ctx.organizationId ? { id: ctx.organizationId } : null), can: { create: () => false, update: (ctx) => hasRole(ctx, "ADMIN"), delete: () => false, }, }, },});Returning null denies the resource for a session without an organization,
which is what you want before the first request resolves one.
create is not constrained by a read scope: a scoped resource cannot create
unless can.create is explicit. Rows loaded through include are not filtered
by the related model’s policy either; query the related model directly when
that matters.
An insert must receive its tenant ownership from trusted server-side context.
Dashild does not copy organizationId from the scope into new data. Before
enabling create, make sure your server-side create flow sets the active
organization itself; do not rely on a hidden form field or a value supplied by
the browser.
Custom auth
Section titled “Custom auth”If you do not use the generated session, return the tenant from
getCurrentUser, or resolve it with organizationOf and rolesOf in
policies.ts:
export const policies = definePolicies<CurrentUser>({ organizationOf: async (user) => user.activeOrganizationId ?? null, rolesOf: async (user) => user.roles ?? [], resources: { // ... },});organizationOf and rolesOf replace the values from user; they are not a
fallback chain.
Reference
Section titled “Reference”apps/playground in the repository is the end-to-end example: two
organizations, an admin who can switch, a member pinned to their own, and
policies that return null without a tenant.