|
| 1 | +# @objectql/better-auth |
| 2 | + |
| 3 | +Better-Auth integration package for ObjectQL, providing multi-tenant organization management capabilities. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This package provides object definitions that align with [Better-Auth's](https://better-auth.com) organization plugin schema, enabling multi-tenant organization management in ObjectQL applications. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +### Authentication Objects |
| 12 | + |
| 13 | +- **user** - System users for authentication |
| 14 | +- **account** - External authentication accounts (OAuth, GitHub, Google, etc.) |
| 15 | +- **session** - User authentication sessions with token management |
| 16 | +- **verification** - Email and phone verification tokens |
| 17 | + |
| 18 | +### Organization Management |
| 19 | + |
| 20 | +- **organization** - Multi-tenant organizations with names, slugs, and metadata |
| 21 | +- **member** - Organization membership with role-based access control |
| 22 | +- **invitation** - Organization invitation system with expiration and status tracking |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +### Server Configuration |
| 27 | + |
| 28 | +The organization plugin is enabled in the server's auth configuration: |
| 29 | + |
| 30 | +```typescript |
| 31 | +import { betterAuth } from "better-auth"; |
| 32 | +import { organization } from "better-auth/plugins"; |
| 33 | + |
| 34 | +const auth = betterAuth({ |
| 35 | + database: pool, |
| 36 | + plugins: [ |
| 37 | + organization({ |
| 38 | + // Enable dynamic access control |
| 39 | + dynamicAccessControl: { |
| 40 | + enabled: true |
| 41 | + }, |
| 42 | + // Enable teams feature |
| 43 | + teams: { |
| 44 | + enabled: true |
| 45 | + }, |
| 46 | + // Define organization roles |
| 47 | + roles: { |
| 48 | + owner: { |
| 49 | + name: 'Owner', |
| 50 | + permissions: ['*'] |
| 51 | + }, |
| 52 | + admin: { |
| 53 | + name: 'Admin', |
| 54 | + permissions: [ |
| 55 | + 'organization:read', |
| 56 | + 'organization:update', |
| 57 | + 'member:*', |
| 58 | + 'invitation:*' |
| 59 | + ] |
| 60 | + }, |
| 61 | + member: { |
| 62 | + name: 'Member', |
| 63 | + permissions: [ |
| 64 | + 'organization:read', |
| 65 | + 'member:read' |
| 66 | + ] |
| 67 | + } |
| 68 | + } |
| 69 | + }) |
| 70 | + ] |
| 71 | +}); |
| 72 | +``` |
| 73 | + |
| 74 | +### Object Definitions |
| 75 | + |
| 76 | +The package includes YAML object definitions that can be loaded by ObjectQL's metadata loader: |
| 77 | + |
| 78 | +```typescript |
| 79 | +import { BetterAuthPackage, getAllObjectDefinitionPaths } from '@objectql/better-auth'; |
| 80 | + |
| 81 | +// Get all object definition paths |
| 82 | +const objectPaths = getAllObjectDefinitionPaths(); |
| 83 | +``` |
| 84 | + |
| 85 | +## Organization API Endpoints |
| 86 | + |
| 87 | +When the organization plugin is enabled, Better-Auth provides the following endpoints: |
| 88 | + |
| 89 | +### Organization Management |
| 90 | + |
| 91 | +- `POST /api/auth/organization/create` - Create a new organization |
| 92 | +- `POST /api/auth/organization/update` - Update organization details |
| 93 | +- `DELETE /api/auth/organization/delete` - Delete an organization |
| 94 | +- `POST /api/auth/organization/set-active` - Set active organization for session |
| 95 | +- `GET /api/auth/organization/get-full` - Get full organization details |
| 96 | +- `GET /api/auth/organization/list` - List user's organizations |
| 97 | + |
| 98 | +### Member Management |
| 99 | + |
| 100 | +- `POST /api/auth/organization/add-member` - Add member to organization |
| 101 | +- `DELETE /api/auth/organization/remove-member` - Remove member from organization |
| 102 | +- `POST /api/auth/organization/update-member-role` - Update member's role |
| 103 | +- `GET /api/auth/organization/list-members` - List organization members |
| 104 | +- `GET /api/auth/organization/get-active-member` - Get current member details |
| 105 | +- `POST /api/auth/organization/leave` - Leave organization |
| 106 | + |
| 107 | +### Invitation Management |
| 108 | + |
| 109 | +- `POST /api/auth/organization/invitation/create` - Create invitation |
| 110 | +- `POST /api/auth/organization/invitation/cancel` - Cancel invitation |
| 111 | +- `POST /api/auth/organization/invitation/accept` - Accept invitation |
| 112 | +- `POST /api/auth/organization/invitation/reject` - Reject invitation |
| 113 | +- `GET /api/auth/organization/invitation/get` - Get invitation details |
| 114 | +- `GET /api/auth/organization/invitation/list` - List organization invitations |
| 115 | + |
| 116 | +## Roles and Permissions |
| 117 | + |
| 118 | +### Default Roles |
| 119 | + |
| 120 | +- **owner** - Full access to all organization features |
| 121 | +- **admin** - Management access for members and invitations |
| 122 | +- **member** - Read-only access to organization and members |
| 123 | + |
| 124 | +### Permission Format |
| 125 | + |
| 126 | +Permissions follow the pattern: `resource:action` |
| 127 | + |
| 128 | +Examples: |
| 129 | +- `organization:read` - Read organization details |
| 130 | +- `organization:update` - Update organization |
| 131 | +- `member:create` - Add members |
| 132 | +- `member:delete` - Remove members |
| 133 | +- `invitation:create` - Create invitations |
| 134 | +- `*` - All permissions (owner only) |
| 135 | + |
| 136 | +## Schema Reference |
| 137 | + |
| 138 | +### Organization |
| 139 | + |
| 140 | +```yaml |
| 141 | +name: organization |
| 142 | +fields: |
| 143 | + name: string (required) - Organization name |
| 144 | + slug: string (required, unique) - URL-friendly identifier |
| 145 | + logo: string - Organization logo URL |
| 146 | + metadata: json - Additional metadata |
| 147 | + createdAt: datetime |
| 148 | + updatedAt: datetime |
| 149 | +``` |
| 150 | +
|
| 151 | +### Member |
| 152 | +
|
| 153 | +```yaml |
| 154 | +name: member |
| 155 | +fields: |
| 156 | + organizationId: string (required) - Organization ID |
| 157 | + userId: string (required) - User ID |
| 158 | + role: string (required) - Member role (owner, admin, member) |
| 159 | + createdAt: datetime |
| 160 | + updatedAt: datetime |
| 161 | +``` |
| 162 | +
|
| 163 | +### Invitation |
| 164 | +
|
| 165 | +```yaml |
| 166 | +name: invitation |
| 167 | +fields: |
| 168 | + organizationId: string (required) - Organization ID |
| 169 | + email: string (required) - Invitee email |
| 170 | + role: string (required) - Role to assign |
| 171 | + status: string - Status (pending, accepted, rejected) |
| 172 | + expiresAt: datetime - Expiration time |
| 173 | + inviterId: string (required) - Inviter user ID |
| 174 | + createdAt: datetime |
| 175 | + updatedAt: datetime |
| 176 | +``` |
| 177 | +
|
| 178 | +## Resources |
| 179 | +
|
| 180 | +- [Better-Auth Documentation](https://better-auth.com) |
| 181 | +- [Better-Auth Organization Plugin](https://better-auth.com/docs/plugins/organization) |
| 182 | +- [ObjectQL Documentation](https://github.com/objectql/objectql) |
| 183 | +
|
| 184 | +## License |
| 185 | +
|
| 186 | +MIT |
0 commit comments