Skip to content

Schema ​

The plugin extends the Better Auth database schema via mergeSchema.

tenant ​

FieldTypeNotes
idstringPrimary key (adapter default)
namestringRequired, sortable
slugstringRequired, unique, sortable
ownerIdstringOptional FK → user.id, indexed, onDelete set null
metadatastringOptional JSON string
createdAtdateDefault: now
updatedAtdateDefault: now, updated on change

tenantMember ​

Platform users who manage a tenant (not tenant end-users).

FieldTypeNotes
idstringPrimary key
tenantIdstringFK → tenant.id, cascade delete, indexed
userIdstringFK → user.id, cascade delete, indexed
rolestringowner | admin | member (default)
createdAtdateDefault: now

Add a unique index on (tenantId, userId) in your ORM (Better Auth does not emit composite uniques).

tenantInvite ​

Invites for tenant end-users to sign up via email when invite-only sign-up is enabled.

FieldTypeNotes
idstringPrimary key
tenantIdstringFK → tenant.id, cascade delete, indexed
emailstringInvited email, indexed
tokenstringUnique invite token
invitedBystringFK → user.id (platform user), optional, indexed
expiresAtdateExpiration time
consumedAtdateSet when used for sign-up
revokedAtdateSet when revoked by an admin
createdAtdateDefault: now

tenantOauthConfig ​

FieldTypeNotes
idstringPrimary key
tenantIdstringFK → tenant.id, cascade delete
providerIdstringBuilt-in provider id (e.g. google)
clientIdstringEncrypted at rest
clientSecretstringEncrypted at rest, never returned by API
scopesstringComma-separated scopes
redirectURIstringOptional override
enabledbooleanDefault true
createdAtdateDefault: now
updatedAtdateDefault: now, updated on change

Add a unique index on (tenantId, providerId) in your ORM.

Extended core tables ​

user ​

Adds:

FieldTypeNotes
tenantIdstringFK → tenant.id, cascade delete, indexed

When keepEmailGloballyUnique is false (default), the global unique constraint on email is removed. You must add composite / partial unique indexes in your database — Better Auth’s schema DSL cannot emit them:

sql
-- Platform users (tenant_id IS NULL)
CREATE UNIQUE INDEX user_email_platform_unique
  ON "user" (email) WHERE tenant_id IS NULL;

-- Tenant users
CREATE UNIQUE INDEX user_email_tenant_unique
  ON "user" (email, tenant_id) WHERE tenant_id IS NOT NULL;

The Next.js demo’s Drizzle schema includes these indexes.

session ​

Adds:

FieldTypeNotes
tenantIdstringFK → tenant.id, cascade delete, indexed

account ​

Adds:

FieldTypeNotes
tenantIdstringFK → tenant.id, cascade delete, indexed

verification ​

Adds:

FieldTypeNotes
tenantIdstringFK → tenant.id, cascade delete, indexed

Tenant sign-up / sign-in verification tokens are JWTs carrying the tenant id as a claim (see Email auth) and are verified via GET /tenant/verify-email without ever writing to this table. verification.tenantId remains available for adapters/flows that do persist verification rows (e.g. a custom sendVerificationEmail that stores its own record).

Custom schema ​

Pass a schema option to rename models or fields:

ts
tenantAuth({
  schema: {
    tenant: {
      modelName: "organization",
    },
  },
});

The plugin merges your overrides with the default schema. Foreign key references respect custom model names.

TypeScript types ​

ts
type TenantRole = "owner" | "admin" | "member";

interface Tenant {
  id: string;
  name: string;
  slug: string;
  ownerId?: string | null;
  metadata?: string | null;
  createdAt: Date;
  updatedAt: Date;
}

interface TenantMember {
  id: string;
  tenantId: string;
  userId: string;
  role: TenantRole;
  createdAt: Date;
}

interface TenantInvite {
  id: string;
  tenantId: string;
  email: string;
  token: string;
  invitedBy?: string | null;
  expiresAt: Date;
  consumedAt?: Date | null;
  revokedAt?: Date | null;
  createdAt: Date;
}

interface TenantOAuthConfig {
  id: string;
  tenantId: string;
  providerId: string;
  clientId: string;
  clientSecret: string;
  scopes?: string | null;
  redirectURI?: string | null;
  enabled: boolean;
  createdAt: Date;
  updatedAt: Date;
}

Exported from better-auth-tenancy and better-auth-tenancy/client.

Released under the MIT License.