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
metadatastringOptional JSON string
createdAtdateDefault: now
updatedAtdateDefault: now, updated on change

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

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. Per-tenant email uniqueness is enforced by plugin endpoints.

session

Adds:

FieldTypeNotes
tenantIdstringFK → tenant.id, cascade delete, indexed

account

Adds:

FieldTypeNotes
tenantIdstringFK → tenant.id, cascade delete, indexed

verification

Adds:

FieldTypeNotes
tenantIdstringIndexed

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
interface Tenant {
  id: string;
  name: string;
  slug: string;
  metadata?: string | null;
  createdAt: Date;
  updatedAt: 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.