Skip to content

Commit 1cddee4

Browse files
Copilothotlong
andcommitted
Document ObjectQL-based auth implementation
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent e17abf0 commit 1cddee4

1 file changed

Lines changed: 71 additions & 8 deletions

File tree

packages/plugins/plugin-auth/README.md

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Authentication & Identity Plugin for ObjectStack.
44

5-
> **✨ Status:** Better-Auth library successfully integrated! Core authentication structure is in place with better-auth v1.4.18. Full API integration and advanced features are in active development.
5+
> **✨ Status:** ObjectQL-based authentication implementation! Uses ObjectQL for data persistence (no third-party ORM required). Core authentication structure is in place with better-auth v1.4.18.
66
77
## Features
88

@@ -12,6 +12,7 @@ Authentication & Identity Plugin for ObjectStack.
1212
- ✅ Service registration in ObjectKernel
1313
- ✅ Configuration schema support
1414
-**Better-Auth library integration (v1.4.18)**
15+
-**ObjectQL-based database implementation (no ORM required)**
1516
-**Direct request forwarding to better-auth handler**
1617
-**Wildcard routing (`/api/v1/auth/*`)**
1718
-**Full better-auth API access via `auth.api`**
@@ -28,10 +29,17 @@ Authentication & Identity Plugin for ObjectStack.
2829
-**Magic Links** - Passwordless authentication (when enabled)
2930
-**Organizations** - Multi-tenant support (when enabled)
3031

31-
### In Active Development
32-
- 🔄 **Database Adapter** - Drizzle ORM integration for data persistence
32+
### ObjectQL-Based Database Architecture
33+
-**Native ObjectQL Data Persistence** - Uses ObjectQL's IDataEngine interface
34+
-**No Third-Party ORM** - No dependency on drizzle-orm or other ORMs
35+
-**Object Definitions** - Auth objects defined using ObjectStack's Object Protocol
36+
- `auth_user` - User accounts
37+
- `auth_session` - Active sessions
38+
- `auth_account` - OAuth provider accounts
39+
- `auth_verification` - Email/phone verification tokens
40+
-**ObjectQL Adapter** - Custom adapter bridges better-auth to ObjectQL
3341

34-
The plugin uses [better-auth](https://www.better-auth.com/) for robust, production-ready authentication functionality. All requests are forwarded directly to better-auth's universal handler, ensuring full compatibility with all better-auth features.
42+
The plugin uses [better-auth](https://www.better-auth.com/) for robust, production-ready authentication functionality. All requests are forwarded directly to better-auth's universal handler, ensuring full compatibility with all better-auth features. Data persistence is handled by ObjectQL, adhering to ObjectStack's "Data as Code" philosophy.
3543

3644
## Installation
3745

@@ -41,18 +49,22 @@ pnpm add @objectstack/plugin-auth
4149

4250
## Usage
4351

44-
### Basic Setup
52+
### Basic Setup with ObjectQL
4553

4654
```typescript
4755
import { ObjectKernel } from '@objectstack/core';
4856
import { AuthPlugin } from '@objectstack/plugin-auth';
57+
import { ObjectQL } from '@objectstack/objectql';
58+
59+
// Initialize ObjectQL as the data engine
60+
const dataEngine = new ObjectQL();
4961

5062
const kernel = new ObjectKernel({
5163
plugins: [
5264
new AuthPlugin({
5365
secret: process.env.AUTH_SECRET,
5466
baseUrl: 'http://localhost:3000',
55-
databaseUrl: process.env.DATABASE_URL,
67+
// ObjectQL will be automatically injected by the kernel
5668
providers: [
5769
{
5870
id: 'google',
@@ -65,13 +77,14 @@ const kernel = new ObjectKernel({
6577
});
6678
```
6779

80+
**Note:** The `databaseUrl` parameter is no longer used. The plugin now uses ObjectQL's IDataEngine interface, which is provided by the kernel's `data` service. This allows the plugin to work with any ObjectQL-compatible driver (memory, SQL, NoSQL, etc.) without requiring a specific ORM.
81+
6882
### With Organization Support
6983

7084
```typescript
7185
new AuthPlugin({
7286
secret: process.env.AUTH_SECRET,
7387
baseUrl: 'http://localhost:3000',
74-
databaseUrl: process.env.DATABASE_URL,
7588
plugins: {
7689
organization: true, // Enable organization/teams
7790
twoFactor: true, // Enable 2FA
@@ -142,10 +155,12 @@ This package provides authentication services powered by better-auth. Current im
142155
7. ✅ Full better-auth API support
143156
8. ✅ OAuth providers (configurable)
144157
9. ✅ 2FA, passkeys, magic links (configurable)
145-
10. 🔄 Database adapter integration (in progress)
158+
10. ✅ ObjectQL-based database implementation (no ORM required)
146159

147160
### Architecture
148161

162+
#### Request Flow
163+
149164
The plugin uses a **direct forwarding** approach:
150165

151166
```typescript
@@ -164,6 +179,54 @@ This architecture provides:
164179
-**Type safety** - Full TypeScript support from better-auth
165180
-**Programmatic API** - Access auth methods via `authManager.api`
166181

182+
#### ObjectQL Database Architecture
183+
184+
The plugin uses **ObjectQL** for data persistence instead of third-party ORMs:
185+
186+
```typescript
187+
// Object definitions replace ORM schemas
188+
export const AuthUser = ObjectSchema.create({
189+
name: 'auth_user',
190+
fields: {
191+
id: Field.text({ label: 'User ID', required: true }),
192+
email: Field.email({ label: 'Email', required: true }),
193+
name: Field.text({ label: 'Name', required: true }),
194+
// ... other fields
195+
},
196+
indexes: [
197+
{ fields: ['email'], unique: true }
198+
]
199+
});
200+
```
201+
202+
**Benefits:**
203+
-**No ORM Dependencies** - No drizzle-orm, Prisma, or other ORMs required
204+
-**Unified Data Layer** - Uses same data engine as rest of ObjectStack
205+
-**Driver Agnostic** - Works with memory, SQL, NoSQL via ObjectQL drivers
206+
-**Type-Safe** - Zod-based schemas provide runtime + compile-time safety
207+
-**"Data as Code"** - Object definitions are versioned, declarative code
208+
-**Metadata Driven** - Supports migrations, validation, indexing via metadata
209+
210+
**Database Objects:**
211+
- `auth_user` - User accounts (email, name, emailVerified, etc.)
212+
- `auth_session` - Active sessions (token, expiresAt, ipAddress, etc.)
213+
- `auth_account` - OAuth provider accounts (providerId, tokens, etc.)
214+
- `auth_verification` - Verification tokens (email, phone verification)
215+
216+
**Adapter:**
217+
The `createObjectQLAdapter()` function bridges better-auth's database interface to ObjectQL's IDataEngine:
218+
219+
```typescript
220+
// Better-auth → ObjectQL Adapter
221+
const adapter = createObjectQLAdapter(dataEngine);
222+
223+
// Better-auth uses this adapter for all database operations
224+
const auth = betterAuth({
225+
database: adapter,
226+
// ... other config
227+
});
228+
```
229+
167230
## Development
168231

169232
```bash

0 commit comments

Comments
 (0)