Installation

Learn how to configure the invite plugin in your project.

GitHubSourcenpmNpm

Install the package

Start by adding the invite plugin to your project:

npm install better-invite

Add the plugin to your auth config

Add the invite plugin to your auth configuration, make sure you also have the plugin admin.

auth.ts
import { betterAuth } from "better-auth"; 
import { admin } from "better-auth/plugins";
import { invite } from "better-invite"; 

export const auth = betterAuth({
  // ... other config options
  plugins: [
    admin({
      ac,
      roles: { user, admin },
      defaultRole: "user",
    }),
    invite({
      defaultRedirectAfterUpgrade: "/auth/invited?token={token}", // Show the user their new role or account
      async sendUserInvitation({ email, role, url, token, newAccount }) {
        if (newAccount)
          void sendInvitationEmail(role as RoleType, email, url); 

        void sendRoleUpgradeEmail(role as RoleType, email, url); 
      }, 
    }), 
  ], 
}); 

Migrate the database

Run the migration or generate the schema to add the necessary fields and tables to the database.

npx @better-auth/cli migrate
npx @better-auth/cli generate

See the Schema section to add the fields manually.

Add the client plugin

Add the client plugin to your better auth client.

import { createAuthClient } from "better-auth/client"; 
import { adminClient } from "better-auth/client/plugins";
import { inviteClient } from "better-invite"; 

const authClient = createAuthClient({
  //... other options
  plugins: [
    adminClient(),
    inviteClient(), 
  ], 
}); 

How is this guide?

Last updated on

On this page