Install the plugin
Add the invite plugin to your Better Auth server configuration:auth.ts
Configuration options
The invite plugin accepts anInviteOptions object with the following configuration:
Basic options
A function to generate the current date. Useful for testing or custom date handling.
The default token type to use:
token: 24-character token generated withgenerateId(24)code: 6-digit alphanumeric codecustom: UsesgenerateTokenfunction
Custom token generation function. Required when using
defaultTokenType: 'custom'.Redirect options
Where to redirect new users to create their account.
Where to redirect existing users to sign in.
Where to redirect users after their role is upgraded. Use
{token} as a placeholder for the invite token.Custom invite URL template. Use
{token} and {callbackUrl} placeholders.Invitation behavior
Token expiration time in seconds (default: 1 hour).
Maximum age in seconds for the invitation cookie used during sign-up flow (default: 10 minutes).
Default maximum number of times an invite can be used. Defaults to 1 for private invites and unlimited for public invites.
Whether to share the inviter’s name with the invitee by default.
How to return the invite to the sender for public invites (when no email is provided).
Default redirect type for public invites.
Cleanup options
Delete invitations when they are rejected or canceled instead of marking them with a status.
Delete invitations after they reach their maximum uses.
Email integration
Function to send invitation emails. Required for private invites (invites with an email address).Parameters:
data.email- Recipient email addressdata.name- Recipient name (may be undefined for new users)data.role- Role the user will receivedata.url- Complete invitation URLdata.token- Invitation tokendata.newAccount-trueif this is a new user,falseif upgrading existing userrequest- The original HTTP request object
Permission controls
Controls who can create invitations.
Example: Function-based permission
Example: Function-based permission
Example: Better Auth admin plugin integration
Example: Better Auth admin plugin integration
Controls who can accept invitations.
Controls who can cancel invitations. Note: Only the user who created the invite can cancel it, regardless of this option.
Controls who can reject invitations. Note: Only the invitee can reject private invites, regardless of this option.
Hooks and callbacks
Lifecycle hooks for invitation events. See Hooks and callbacks for details.
Callback triggered when an invitation is successfully used.
Schema customization
Custom schema modifications for the invite tables. See Better Auth documentation for schema customization.
Database tables
The invite plugin creates two tables in your database:invite table
id- Primary keytoken- Unique invitation tokencreatedAt- Creation timestampexpiresAt- Expiration timestampmaxUses- Maximum number of times this invite can be usedcreatedByUserId- ID of the user who created the inviteredirectToAfterUpgrade- Custom redirect URL after upgradeshareInviterName- Whether to share inviter nameemail- Email for private invites (optional)role- Role to assign to invited usernewAccount- Whether this is for a new account (private invites only)status- Invitation status:pending,rejected,canceled, orused
inviteUse table
id- Primary keyinviteId- Reference to inviteusedAt- Timestamp when invite was usedusedByUserId- ID of user who used the invite
The plugin automatically handles schema migration when you add it to your Better Auth configuration.
Complete example
auth.ts
Next steps
Client setup
Set up the invite plugin on your client
Creating invites
Learn how to create invitations