invite and inviteUse.
Schema Overview
The schema is defined insrc/schema.ts and follows Better Auth’s plugin schema specification.
Invite Table
Theinvite table stores invitation records.
Schema Definition
Fields Reference
Primary key (auto-generated).
Unique invitation token. Generated based on
defaultTokenType configuration.Constraint: Unique across all invitations.Timestamp when the invitation was created.
Timestamp when the invitation expires. Calculated using
invitationTokenExpiresIn configuration.Maximum number of times this invitation can be used.Default: 1 for private invites, infinite for public invites.
Foreign key to the
user table. References the user who created the invitation.References: user.idOn Delete: SET NULLOptional URL to redirect to after the user accepts the invitation and upgrades their role.
Whether to share the inviter’s name with the invitee.Default: Controlled by
defaultShareInviterName configuration.Email address for private invitations. When set, only users with this email can accept the invitation.Null: Indicates a public invitation.
The role to assign to the user upon accepting the invitation.
Indicates whether this invitation is for a new account creation. Only applicable to private invitations.
Current status of the invitation.Values:
pending: Invitation is active and can be acceptedrejected: Invitation was rejected by the inviteecanceled: Invitation was canceled by the inviterused: Invitation has reached its maximum uses
Invitation Status Lifecycle
Invitations can be automatically deleted based on
cleanupInvitesAfterMaxUses and cleanupInvitesOnDecision configuration options.InviteUse Table
TheinviteUse table tracks each time an invitation is used.
Schema Definition
Fields Reference
Primary key (auto-generated).
Foreign key to the
invite table. References the invitation that was used.References: invite.idOn Delete: SET NULLTimestamp when the invitation was accepted/used.
Foreign key to the
user table. References the user who accepted the invitation.References: user.idOn Delete: SET NULLRelationships
The schema defines several relationships between tables:Invite → User (Creator)
InviteUse → Invite
maxUses > 1.
InviteUse → User (Acceptor)
Database Migrations
Initial Migration
Better Auth automatically generates migrations for plugin schemas. After adding the invite plugin, run:invite and inviteUse tables in your database.
Example Migration (SQL)
The generated migration will look similar to this:Custom Schema Extensions
You can extend the schema using theschema configuration option:
Querying the Schema
Get Invitations by User
Get Invitation Usage Count
Check Invitation Status
Cleanup Strategies
The plugin implements cleanup logic insrc/utils.ts:161-172:
Cleanup on Max Uses
WhencleanupInvitesAfterMaxUses is enabled:
- All
inviteUserecords are deleted - The
inviterecord is deleted - No
inviteUserecord is created for the final use
Cleanup on Decision
WhencleanupInvitesOnDecision is enabled:
- Invitations with status
rejectedorcanceledare deleted - Associated
inviteUserecords are preserved (if any existed before the decision)
See Configuration for more details on cleanup options.