|
| 1 | +# Multi-Tenant Organization Management Implementation |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This implementation adds multi-tenant organization management capabilities to ObjectQL using the Better-Auth library and its organization plugin, following Better-Auth's official specifications. |
| 6 | + |
| 7 | +## Implementation Details |
| 8 | + |
| 9 | +### 1. Better-Auth Plugin Integration |
| 10 | + |
| 11 | +**File:** `packages/server/src/auth/auth.client.ts` |
| 12 | + |
| 13 | +The organization plugin has been enabled in the Better-Auth configuration with the following features: |
| 14 | + |
| 15 | +- **Dynamic Access Control**: Enabled for fine-grained permission management |
| 16 | +- **Teams**: Enabled for sub-organization grouping |
| 17 | +- **Role-Based Access Control**: Three default roles configured: |
| 18 | + - `owner`: Full access to all organization features |
| 19 | + - `admin`: Management access for members, invitations, and organization updates |
| 20 | + - `member`: Read-only access to organization and members |
| 21 | + |
| 22 | +### 2. Object Schema Definitions |
| 23 | + |
| 24 | +**Location:** `packages/better-auth/src/*.object.yml` |
| 25 | + |
| 26 | +Seven object schemas are provided, aligned with Better-Auth's database schema: |
| 27 | + |
| 28 | +1. **user.object.yml** - System users with authentication fields |
| 29 | +2. **account.object.yml** - External authentication providers (OAuth) |
| 30 | +3. **session.object.yml** - User sessions with token management |
| 31 | +4. **verification.object.yml** - Email/phone verification tokens |
| 32 | +5. **organization.object.yml** - Multi-tenant organizations |
| 33 | +6. **member.object.yml** - Organization membership with roles |
| 34 | +7. **invitation.object.yml** - Organization invitations with expiration |
| 35 | + |
| 36 | +### 3. Package Exports |
| 37 | + |
| 38 | +**File:** `packages/better-auth/src/index.ts` |
| 39 | + |
| 40 | +The package now exports: |
| 41 | +- `BetterAuthPackage` - Package metadata |
| 42 | +- `objectDefinitions` - List of all object definition files |
| 43 | +- `getObjectDefinitionPath(filename)` - Get path to specific object definition |
| 44 | +- `getAllObjectDefinitionPaths()` - Get paths to all object definitions |
| 45 | + |
| 46 | +### 4. API Endpoints |
| 47 | + |
| 48 | +All organization management endpoints are automatically exposed through Better-Auth at `/api/auth/`: |
| 49 | + |
| 50 | +#### Organization Management (10 endpoints) |
| 51 | +- `POST /api/auth/organization/create` - Create organization |
| 52 | +- `POST /api/auth/organization/update` - Update organization |
| 53 | +- `DELETE /api/auth/organization/delete` - Delete organization |
| 54 | +- `POST /api/auth/organization/set-active` - Set active organization |
| 55 | +- `GET /api/auth/organization/get-full` - Get full organization details |
| 56 | +- `GET /api/auth/organization/list` - List user's organizations |
| 57 | +- `GET /api/auth/organization/check-slug` - Check slug availability |
| 58 | + |
| 59 | +#### Member Management (8 endpoints) |
| 60 | +- `POST /api/auth/organization/add-member` - Add member |
| 61 | +- `DELETE /api/auth/organization/remove-member` - Remove member |
| 62 | +- `POST /api/auth/organization/update-member-role` - Update member role |
| 63 | +- `GET /api/auth/organization/list-members` - List members |
| 64 | +- `GET /api/auth/organization/get-active-member` - Get current member |
| 65 | +- `GET /api/auth/organization/get-active-member-role` - Get member role |
| 66 | +- `POST /api/auth/organization/leave` - Leave organization |
| 67 | + |
| 68 | +#### Invitation Management (7 endpoints) |
| 69 | +- `POST /api/auth/organization/invitation/create` - Create invitation |
| 70 | +- `POST /api/auth/organization/invitation/cancel` - Cancel invitation |
| 71 | +- `POST /api/auth/organization/invitation/accept` - Accept invitation |
| 72 | +- `POST /api/auth/organization/invitation/reject` - Reject invitation |
| 73 | +- `GET /api/auth/organization/invitation/get` - Get invitation details |
| 74 | +- `GET /api/auth/organization/invitation/list` - List organization invitations |
| 75 | +- `GET /api/auth/organization/invitation/list-user` - List user invitations |
| 76 | + |
| 77 | +#### Team Management (10 endpoints) |
| 78 | +- `POST /api/auth/organization/team/create` - Create team |
| 79 | +- `POST /api/auth/organization/team/update` - Update team |
| 80 | +- `DELETE /api/auth/organization/team/remove` - Remove team |
| 81 | +- `POST /api/auth/organization/team/set-active` - Set active team |
| 82 | +- `GET /api/auth/organization/team/list` - List organization teams |
| 83 | +- `GET /api/auth/organization/team/list-user` - List user teams |
| 84 | +- `GET /api/auth/organization/team/list-members` - List team members |
| 85 | +- `POST /api/auth/organization/team/add-member` - Add team member |
| 86 | +- `DELETE /api/auth/organization/team/remove-member` - Remove team member |
| 87 | + |
| 88 | +#### Access Control (5 endpoints) |
| 89 | +- `POST /api/auth/organization/role/create` - Create custom role |
| 90 | +- `POST /api/auth/organization/role/update` - Update role |
| 91 | +- `DELETE /api/auth/organization/role/delete` - Delete role |
| 92 | +- `GET /api/auth/organization/role/list` - List roles |
| 93 | +- `GET /api/auth/organization/role/get` - Get role details |
| 94 | +- `POST /api/auth/organization/has-permission` - Check permission |
| 95 | + |
| 96 | +**Total: 36 organization management endpoints** |
| 97 | + |
| 98 | +## Role-Based Access Control |
| 99 | + |
| 100 | +### Permission Model |
| 101 | + |
| 102 | +Roles are defined using the `role()` function from Better-Auth's access control plugin: |
| 103 | + |
| 104 | +```typescript |
| 105 | +import { role } from "better-auth/plugins/access"; |
| 106 | + |
| 107 | +const ownerRole = role({ |
| 108 | + organization: ['create', 'read', 'update', 'delete'], |
| 109 | + member: ['create', 'read', 'update', 'delete'], |
| 110 | + invitation: ['create', 'read', 'delete'], |
| 111 | + team: ['create', 'read', 'update', 'delete'] |
| 112 | +}); |
| 113 | +``` |
| 114 | + |
| 115 | +### Default Roles |
| 116 | + |
| 117 | +1. **Owner** |
| 118 | + - Full access to all organization resources |
| 119 | + - Can manage organization settings, members, invitations, and teams |
| 120 | + - Actions: create, read, update, delete |
| 121 | + |
| 122 | +2. **Admin** |
| 123 | + - Can update organization settings |
| 124 | + - Full member and invitation management |
| 125 | + - Can manage teams |
| 126 | + - Cannot delete organization |
| 127 | + |
| 128 | +3. **Member** |
| 129 | + - Read-only access to organization and members |
| 130 | + - Can view teams |
| 131 | + - Cannot modify anything |
| 132 | + |
| 133 | +## Database Schema |
| 134 | + |
| 135 | +The organization plugin automatically creates the following database tables: |
| 136 | + |
| 137 | +- `organization` - Organization details |
| 138 | +- `member` - Organization memberships |
| 139 | +- `invitation` - Pending invitations |
| 140 | +- `team` - Organization teams (if enabled) |
| 141 | +- `teamMember` - Team memberships (if enabled) |
| 142 | +- `organizationRole` - Custom roles (if dynamic access control enabled) |
| 143 | + |
| 144 | +These tables integrate with Better-Auth's core tables (user, account, session, verification). |
| 145 | + |
| 146 | +## Testing & Verification |
| 147 | + |
| 148 | +Two verification scripts are provided in `packages/server/scripts/`: |
| 149 | + |
| 150 | +1. **verify-organization-plugin.js** - Quick verification that plugin is loaded |
| 151 | +2. **inspect-organization.js** - Detailed inspection of all endpoints and plugin configuration |
| 152 | + |
| 153 | +Run them after building: |
| 154 | +```bash |
| 155 | +cd packages/server |
| 156 | +npm run build |
| 157 | +node scripts/verify-organization-plugin.js |
| 158 | +node scripts/inspect-organization.js |
| 159 | +``` |
| 160 | + |
| 161 | +## Documentation |
| 162 | + |
| 163 | +Comprehensive documentation is available in: |
| 164 | +- `packages/better-auth/README.md` - Full API reference and usage guide |
| 165 | + |
| 166 | +## Security Considerations |
| 167 | + |
| 168 | +✅ **Security scan passed**: No vulnerabilities detected by CodeQL |
| 169 | +✅ **Code review**: Minor suggestions for test scripts only, production code is clean |
| 170 | +✅ **TypeScript**: All code is fully typed and type-safe |
| 171 | +✅ **Better-Auth**: Uses official library with security best practices built-in |
| 172 | + |
| 173 | +## Next Steps |
| 174 | + |
| 175 | +To use this implementation: |
| 176 | + |
| 177 | +1. Set up a PostgreSQL database |
| 178 | +2. Configure `DATABASE_URL` environment variable |
| 179 | +3. Better-Auth will automatically create the necessary tables on first use |
| 180 | +4. Use the organization endpoints to manage multi-tenant operations |
| 181 | + |
| 182 | +## References |
| 183 | + |
| 184 | +- [Better-Auth Documentation](https://better-auth.com) |
| 185 | +- [Better-Auth Organization Plugin](https://better-auth.com/docs/plugins/organization) |
| 186 | +- [ObjectQL Documentation](https://github.com/objectql/objectql) |
0 commit comments