| 1 | --- |
| 2 | name: organization-best-practices |
| 3 | description: Configure multi-tenant organizations, manage members and invitations, define custom roles and permissions, set up teams, and implement RBAC using Better Auth's organization plugin. Use when users need org setup, team management, member roles, access control, or the Better Auth organization plugin. |
| 4 | --- |
| 5 | |
| 6 | ## Setup |
| 7 | |
| 8 | 1. Add `organization()` plugin to server config |
| 9 | 2. Add `organizationClient()` plugin to client config |
| 10 | 3. Run `npx @better-auth/cli migrate` |
| 11 | 4. Verify: check that organization, member, invitation tables exist in your database |
| 12 | |
| 13 | ```ts |
| 14 | import { betterAuth } |
| 15 | import { organization } |
| 16 | |
| 17 | export const auth = |
| 18 | plugins: [ |
| 19 | organization({ |
| 20 | allowUserToCreateOrganization: true, |
| 21 | organizationLimit: 5, // |
| 22 | membershipLimit: 100, // |
| 23 | }), |
| 24 | ], |
| 25 | }); |
| 26 | ``` |
| 27 | |
| 28 | ### Client-Side Setup |
| 29 | |
| 30 | ```ts |
| 31 | import { createAuthClient } |
| 32 | import { organizationClient } |
| 33 | |
| 34 | export const authClient = |
| 35 | plugins: [organizationClient()], |
| 36 | }); |
| 37 | ``` |
| 38 | |
| 39 | ## Creating Organizations |
| 40 | |
| 41 | The creator is automatically assigned the `owner` role. |
| 42 | |
| 43 | ```ts |
| 44 | const createOrg = async |
| 45 | const { data, |
| 46 | name: "My Company", |
| 47 | slug: "my-company", |
| 48 | logo: "https://example.com/logo.png", |
| 49 | metadata: { plan: " |
| 50 | }); |
| 51 | }; |
| 52 | ``` |
| 53 | |
| 54 | ### Controlling Organization Creation |
| 55 | |
| 56 | Restrict who can create organizations based on user attributes: |
| 57 | |
| 58 | ```ts |
| 59 | organization({ |
| 60 | allowUserToCreateOrganization: async ( |
| 61 | return user.emailVerified |
| 62 | }, |
| 63 | organizationLimit: async ( |
| 64 | // Premium users get more organizations |
| 65 | return user.plan |
| 66 | }, |
| 67 | }); |
| 68 | ``` |
| 69 | |
| 70 | ### Creating Organizations on Behalf of Users |
| 71 | |
| 72 | Administrators can create organizations for other users (server-side only): |
| 73 | |
| 74 | ```ts |
| 75 | await auth.api. |
| 76 | body: { |
| 77 | name: "Client Organization", |
| 78 | slug: "client-org", |
| 79 | userId: "user-id-who-will-be-owner", // |
| 80 | }, |
| 81 | }); |
| 82 | ``` |
| 83 | |
| 84 | **Note**: The `userId |
| 85 | |
| 86 | |
| 87 | ## Active Organizations |
| 88 | |
| 89 | Stored in the session and scopes subsequent API calls. Set after user selects one. |
| 90 | |
| 91 | ```ts |
| 92 | const setActive = async |
| 93 | const { data, |
| 94 | organizationId, |
| 95 | }); |
| 96 | }; |
| 97 | ``` |
| 98 | |
| 99 | Many endpoints use the active organization when `organizationId` is not provided (`listMembers`, |
| 100 | |
| 101 | Use `getFullOrganization()` to retrieve the active org with all members, invitations, and teams. |
| 102 | |
| 103 | ## Members |
| 104 | |
| 105 | ### Adding Members (Server-Side) |
| 106 | |
| 107 | ```ts |
| 108 | await auth.api. |
| 109 | body: { |
| 110 | userId: "user-id", |
| 111 | role: "member", |
| 112 | organizationId: "org-id", |
| 113 | }, |
| 114 | }); |
| 115 | ``` |
| 116 | |
| 117 | For client-side member additions, use the invitation system instead. |
| 118 | |
| 119 | ### Assigning Multiple Roles |
| 120 | |
| 121 | ```ts |
| 122 | await auth.api. |
| 123 | body: { |
| 124 | userId: "user-id", |
| 125 | role: ["admin", |
| 126 | organizationId: "org-id", |
| 127 | }, |
| 128 | }); |
| 129 | ``` |
| 130 | |
| 131 | ### Removing Members |
| 132 | |
| 133 | Use `removeMember({ memberIdOrEmail })`. The last owner cannot be removed — assign ownership to another member first. |
| 134 | |
| 135 | ### Updating Member Roles |
| 136 | |
| 137 | Use `updateMemberRole({ memberId, role })`. |
| 138 | |
| 139 | ### Membership Limits |
| 140 | |
| 141 | ```ts |
| 142 | organization({ |
| 143 | membershipLimit: async ( |
| 144 | if (organization.metadata |
| 145 | return 1000; |
| 146 | } |
| 147 | return 50; |
| 148 | }, |
| 149 | }); |
| 150 | ``` |
| 151 | |
| 152 | ## Invitations |
| 153 | |
| 154 | ### Setting Up Invitation Emails |
| 155 | |
| 156 | ```ts |
| 157 | import { betterAuth } |
| 158 | import { organization } |
| 159 | import { sendEmail } |
| 160 | |
| 161 | export const auth = |
| 162 | plugins: [ |
| 163 | organization({ |
| 164 | sendInvitationEmail: async ( |
| 165 | const { email, |
| 166 | |
| 167 | await sendEmail({ |
| 168 | to: email, |
| 169 | subject: `Join ${organization |
| 170 | html: ` |
| 171 | <p>${inviter.user.name |
| 172 | <a href="https://yourapp.com/accept-invite?id=${invitation.id}"> |
| 173 | Accept Invitation |
| 174 | </a> |
| 175 | `, |
| 176 | }); |
| 177 | }, |
| 178 | }), |
| 179 | ], |
| 180 | }); |
| 181 | ``` |
| 182 | |
| 183 | ### Sending Invitations |
| 184 | |
| 185 | ```ts |
| 186 | await authClient.organization. |
| 187 | email: "newuser@example.com", |
| 188 | role: "member", |
| 189 | }); |
| 190 | ``` |
| 191 | |
| 192 | ### Shareable Invitation URLs |
| 193 | |
| 194 | ```ts |
| 195 | const { data } |
| 196 | email: "newuser@example.com", |
| 197 | role: "member", |
| 198 | callbackURL: "https://yourapp.com/dashboard", |
| 199 | }); |
| 200 | |
| 201 | // Share data.url via any channel |
| 202 | ``` |
| 203 | |
| 204 | This endpoint does not call `sendInvitationEmail` — handle delivery yourself. |
| 205 | |
| 206 | ### Invitation Configuration |
| 207 | |
| 208 | ```ts |
| 209 | organization({ |
| 210 | invitationExpiresIn: 60 * 60 |
| 211 | invitationLimit: 100, // |
| 212 | cancelPendingInvitationsOnReInvite: true, // |
| 213 | }); |
| 214 | ``` |
| 215 | |
| 216 | ## Roles & Permissions |
| 217 | |
| 218 | Default roles: `owner` (full access), `admin` (manage members/invitations/settings), ` |
| 219 | |
| 220 | ### Checking Permissions |
| 221 | |
| 222 | ```ts |
| 223 | const { data } |
| 224 | permission: "member:write", |
| 225 | }); |
| 226 | |
| 227 | if (data?.hasPermission) |
| 228 | // User can manage members |
| 229 | } |
| 230 | ``` |
| 231 | |
| 232 | Use `checkRolePermission({ role, permissions })` for client-side UI rendering (static only). For dynamic access control, use the `hasPermission` endpoint. |
| 233 | |
| 234 | ## Teams |
| 235 | |
| 236 | ### Enabling Teams |
| 237 | |
| 238 | ```ts |
| 239 | import { organization } |
| 240 | |
| 241 | export const auth = |
| 242 | plugins: [ |
| 243 | organization({ |
| 244 | teams: { |
| 245 | enabled: true |
| 246 | } |
| 247 | }), |
| 248 | ], |
| 249 | }); |
| 250 | ``` |
| 251 | |
| 252 | ### Creating Teams |
| 253 | |
| 254 | ```ts |
| 255 | const { data } |
| 256 | name: "Engineering", |
| 257 | }); |
| 258 | ``` |
| 259 | |
| 260 | ### Managing Team Members |
| 261 | |
| 262 | Use `addTeamMember({ teamId, userId })` (member must be in org first) and `removeTeamMember({ teamId, userId })` (stays in org). |
| 263 | |
| 264 | Set active team with `setActiveTeam({ teamId })`. |
| 265 | |
| 266 | ### Team Limits |
| 267 | |
| 268 | ```ts |
| 269 | organization({ |
| 270 | teams: { |
| 271 | maximumTeams: 20, // |
| 272 | maximumMembersPerTeam: 50, // |
| 273 | allowRemovingAllTeams: false, // |
| 274 | } |
| 275 | }); |
| 276 | ``` |
| 277 | |
| 278 | ## Dynamic Access Control |
| 279 | |
| 280 | ### Enabling Dynamic Access Control |
| 281 | |
| 282 | ```ts |
| 283 | import { organization } |
| 284 | import { dynamicAccessControl } |
| 285 | |
| 286 | export const auth = |
| 287 | plugins: [ |
| 288 | organization({ |
| 289 | dynamicAccessControl: { |
| 290 | enabled: true |
| 291 | } |
| 292 | }), |
| 293 | ], |
| 294 | }); |
| 295 | ``` |
| 296 | |
| 297 | ### Creating Custom Roles |
| 298 | |
| 299 | ```ts |
| 300 | await authClient.organization. |
| 301 | role: "moderator", |
| 302 | permission: { |
| 303 | member: ["read"], |
| 304 | invitation: ["read"], |
| 305 | }, |
| 306 | }); |
| 307 | ``` |
| 308 | |
| 309 | Use `updateRole({ roleId, permission })` and `deleteRole({ roleId })`. Pre-defined roles (owner, admin, member) cannot be deleted. Roles assigned to members cannot be deleted until reassigned. |
| 310 | |
| 311 | ## Lifecycle Hooks |
| 312 | |
| 313 | Execute custom logic at various points in the organization lifecycle: |
| 314 | |
| 315 | ```ts |
| 316 | organization({ |
| 317 | hooks: { |
| 318 | organization: { |
| 319 | beforeCreate: async ( |
| 320 | // Validate or modify data before creation |
| 321 | return { |
| 322 | data: { |
| 323 | ...data, |
| 324 | metadata: { ...data |
| 325 | }, |
| 326 | }; |
| 327 | }, |
| 328 | afterCreate: async ( |
| 329 | // Post-creation logic (e.g., send welcome email, create default resources) |
| 330 | await createDefaultResources(organization |
| 331 | }, |
| 332 | beforeDelete: async ( |
| 333 | // Cleanup before deletion |
| 334 | await archiveOrganizationData(organization |
| 335 | }, |
| 336 | }, |
| 337 | member: { |
| 338 | afterCreate: async ( |
| 339 | await notifyAdmins(organization |
| 340 | }, |
| 341 | }, |
| 342 | invitation: { |
| 343 | afterCreate: async ( |
| 344 | await logInvitation(invitation |
| 345 | }, |
| 346 | }, |
| 347 | }, |
| 348 | }); |
| 349 | ``` |
| 350 | |
| 351 | ## Schema Customization |
| 352 | |
| 353 | Customize table names, field names, and add additional fields: |
| 354 | |
| 355 | ```ts |
| 356 | organization({ |
| 357 | schema: { |
| 358 | organization: { |
| 359 | modelName: "workspace", // |
| 360 | fields: { |
| 361 | name: "workspaceName", // |
| 362 | }, |
| 363 | additionalFields: { |
| 364 | billingId: { |
| 365 | type: "string", |
| 366 | required: false, |
| 367 | }, |
| 368 | }, |
| 369 | }, |
| 370 | member: { |
| 371 | additionalFields: { |
| 372 | department: { |
| 373 | type: "string", |
| 374 | required: false, |
| 375 | }, |
| 376 | title: { |
| 377 | type: "string", |
| 378 | required: false, |
| 379 | }, |
| 380 | }, |
| 381 | }, |
| 382 | }, |
| 383 | }); |
| 384 | ``` |
| 385 | |
| 386 | ## Security Considerations |
| 387 | |
| 388 | ### Owner Protection |
| 389 | |
| 390 | - The last owner cannot be removed from an organization |
| 391 | - The last owner cannot leave the organization |
| 392 | - The owner role cannot be removed from the last owner |
| 393 | |
| 394 | Always ensure ownership transfer before removing the current owner: |
| 395 | |
| 396 | ```ts |
| 397 | // Transfer ownership first |
| 398 | await authClient.organization. |
| 399 | memberId: "new-owner-member-id", |
| 400 | role: "owner", |
| 401 | }); |
| 402 | |
| 403 | // Then the previous owner can be demoted or removed |
| 404 | ``` |
| 405 | |
| 406 | ### Organization Deletion |
| 407 | |
| 408 | Deleting an organization removes all associated data (members, invitations, teams). Prevent accidental deletion: |
| 409 | |
| 410 | ```ts |
| 411 | organization({ |
| 412 | disableOrganizationDeletion: true, // |
| 413 | }); |
| 414 | ``` |
| 415 | |
| 416 | Or implement soft delete via hooks: |
| 417 | |
| 418 | ```ts |
| 419 | organization({ |
| 420 | hooks: { |
| 421 | organization: { |
| 422 | beforeDelete: async ( |
| 423 | // Archive instead of delete |
| 424 | await archiveOrganization(organization |
| 425 | throw new Error( |
| 426 | }, |
| 427 | }, |
| 428 | }, |
| 429 | }); |
| 430 | ``` |
| 431 | |
| 432 | ### Invitation Security |
| 433 | |
| 434 | - Invitations expire after 48 hours by default |
| 435 | - Only the invited email address can accept an invitation |
| 436 | - Pending invitations can be cancelled by organization admins |
| 437 | |
| 438 | ## Complete Configuration Example |
| 439 | |
| 440 | ```ts |
| 441 | import { betterAuth } |
| 442 | import { organization } |
| 443 | import { sendEmail } |
| 444 | |
| 445 | export const auth = |
| 446 | plugins: [ |
| 447 | organization({ |
| 448 | // Organization limits |
| 449 | allowUserToCreateOrganization: true, |
| 450 | organizationLimit: 10, |
| 451 | membershipLimit: 100, |
| 452 | creatorRole: "owner", |
| 453 | |
| 454 | // Slugs |
| 455 | defaultOrganizationIdField: "slug", |
| 456 | |
| 457 | // Invitations |
| 458 | invitationExpiresIn: 60 * 60 |
| 459 | invitationLimit: 50, |
| 460 | sendInvitationEmail: async ( |
| 461 | await sendEmail({ |
| 462 | to: data.email |
| 463 | subject: `Join ${data |
| 464 | html: `<a href="https://app.com/invite/${data |
| 465 | }); |
| 466 | }, |
| 467 | |
| 468 | // Hooks |
| 469 | hooks: { |
| 470 | organization: { |
| 471 | afterCreate: async ( |
| 472 | console.log(`Organization |
| 473 | }, |
| 474 | }, |
| 475 | }, |
| 476 | }), |
| 477 | ], |
| 478 | }); |
| 479 | ``` |