Skip to content

Commit e958451

Browse files
Copilothotlong
andcommitted
Enable organization plugin and export schemas
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 4ce657f commit e958451

File tree

3 files changed

+281
-2
lines changed

3 files changed

+281
-2
lines changed

packages/better-auth/README.md

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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

packages/better-auth/src/index.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,54 @@
1-
// Export nothing for now, just a marker package
1+
import * as path from 'path';
2+
3+
/**
4+
* Better-Auth integration package for ObjectQL
5+
*
6+
* This package provides object definitions for Better-Auth's multi-tenant
7+
* organization management system, including:
8+
*
9+
* - user: System users for authentication
10+
* - account: External authentication accounts (OAuth, etc.)
11+
* - session: User authentication sessions
12+
* - verification: Verification tokens for email/phone
13+
* - organization: Organizations for multi-tenant apps
14+
* - member: Organization membership
15+
* - invitation: Organization invitations
16+
*
17+
* These object definitions follow Better-Auth's schema specifications
18+
* and can be loaded into ObjectQL's metadata registry.
19+
*/
20+
221
export const BetterAuthPackage = {
322
name: '@objectql/better-auth',
423
path: __dirname
524
};
25+
26+
/**
27+
* List of object definition files provided by this package
28+
*/
29+
export const objectDefinitions = [
30+
'user.object.yml',
31+
'account.object.yml',
32+
'session.object.yml',
33+
'verification.object.yml',
34+
'organization.object.yml',
35+
'member.object.yml',
36+
'invitation.object.yml'
37+
];
38+
39+
/**
40+
* Get the full path to an object definition file
41+
* @param filename The object definition filename
42+
* @returns Full path to the file
43+
*/
44+
export function getObjectDefinitionPath(filename: string): string {
45+
return path.join(__dirname, filename);
46+
}
47+
48+
/**
49+
* Get all object definition paths
50+
* @returns Array of full paths to all object definition files
51+
*/
52+
export function getAllObjectDefinitionPaths(): string[] {
53+
return objectDefinitions.map(f => getObjectDefinitionPath(f));
54+
}

packages/server/src/auth/auth.client.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ let authInstance: any;
55
export const getAuth = async () => {
66
if (authInstance) return authInstance;
77
const { betterAuth } = await import("better-auth");
8+
const { organization } = await import("better-auth/plugins");
89

910
try {
1011
const pool = new Pool({
@@ -14,7 +15,50 @@ export const getAuth = async () => {
1415
database: pool,
1516
emailAndPassword: {
1617
enabled: true
17-
}
18+
},
19+
plugins: [
20+
organization({
21+
// Enable role-based access control
22+
dynamicAccessControl: {
23+
enabled: true
24+
},
25+
// Enable teams feature
26+
teams: {
27+
enabled: true
28+
},
29+
// Define default organization roles
30+
roles: {
31+
owner: {
32+
name: 'Owner',
33+
description: 'Organization owner with full access',
34+
permissions: ['*']
35+
},
36+
admin: {
37+
name: 'Admin',
38+
description: 'Administrator with management access',
39+
permissions: [
40+
'organization:read',
41+
'organization:update',
42+
'member:create',
43+
'member:read',
44+
'member:update',
45+
'member:delete',
46+
'invitation:create',
47+
'invitation:read',
48+
'invitation:delete'
49+
]
50+
},
51+
member: {
52+
name: 'Member',
53+
description: 'Regular organization member',
54+
permissions: [
55+
'organization:read',
56+
'member:read'
57+
]
58+
}
59+
}
60+
})
61+
]
1862
});
1963
return authInstance;
2064
} catch (e: any) {

0 commit comments

Comments
 (0)