Installation
1
Add the plugin to your auth config
auth.ts
2
Migrate the database
Run the migration or generate the schema to add the necessary tables.- migrate
- generate
3
Add the client plugin
auth-client.ts
Organizations
Create an organization
allowUserToCreateOrganization:
auth.ts
List organizations
- React
- Server
Active organization
The active organization is the workspace the user is currently working in. Set it with:- React
Get full organization details
Update an organization
Delete an organization
auth.ts
Invitations
Set up invitation emails
Provide asendInvitationEmail callback so Better Auth can send invitation emails:
auth.ts
Invite a member
Accept an invitation
On the page that handles the invitation link:The user must be signed in before accepting an invitation. If
requireEmailVerificationOnInvitation is enabled, their email must also be verified.Other invitation actions
Members
List members
Update a member’s role
Remove a member
Add a member directly (server-only)
Leave an organization
Roles and permissions
Default roles
The organization plugin ships with three built-in roles:A user can have multiple roles. Multiple roles are stored as comma-separated strings in the database.
Default permissions
Custom permissions
1
Create an access controller
permissions.ts
2
Define roles
permissions.ts
3
Check permissions
From the client (async, server-verified):Lifecycle hooks
You can run custom logic before and after organization operations usingorganizationHooks.
Organization hooks
auth.ts
Member hooks
auth.ts
Invitation hooks
auth.ts
Hook error handling
Throwing an error in abefore hook aborts the operation:
Dynamic access control
Dynamic access control lets you create and manage roles at runtime, stored in the database rather than defined in code.auth.ts
auth-client.ts