@delmaredigital/payload-better-auth 0.1.5 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +165 -250
- package/dist/adapter/collections.d.ts +52 -0
- package/dist/adapter/collections.d.ts.map +1 -0
- package/dist/adapter/collections.js +150 -0
- package/dist/adapter/collections.js.map +1 -0
- package/dist/adapter/index.d.ts +6 -9
- package/dist/adapter/index.d.ts.map +1 -0
- package/dist/adapter/index.js +399 -350
- package/dist/adapter/index.js.map +1 -1
- package/dist/components/BeforeLogin.d.ts +11 -0
- package/dist/components/BeforeLogin.d.ts.map +1 -0
- package/dist/components/BeforeLogin.js +25 -0
- package/dist/components/BeforeLogin.js.map +1 -0
- package/dist/components/LoginView.d.ts +21 -0
- package/dist/components/LoginView.d.ts.map +1 -0
- package/dist/components/LoginView.js +214 -0
- package/dist/components/LoginView.js.map +1 -0
- package/dist/components/LogoutButton.d.ts +7 -0
- package/dist/components/LogoutButton.d.ts.map +1 -0
- package/dist/components/LogoutButton.js +43 -0
- package/dist/components/LogoutButton.js.map +1 -0
- package/dist/exports/client.d.ts +6 -0
- package/dist/exports/client.d.ts.map +1 -0
- package/dist/exports/client.js +6 -0
- package/dist/exports/client.js.map +1 -0
- package/dist/exports/components.d.ts +12 -0
- package/dist/exports/components.d.ts.map +1 -0
- package/dist/exports/components.js +10 -0
- package/dist/exports/components.js.map +1 -0
- package/dist/index.d.ts +14 -115
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -610
- package/dist/index.js.map +1 -1
- package/dist/plugin/index.d.ts +68 -16
- package/dist/plugin/index.d.ts.map +1 -0
- package/dist/plugin/index.js +268 -76
- package/dist/plugin/index.js.map +1 -1
- package/dist/utils/detectAuthConfig.d.ts +18 -0
- package/dist/utils/detectAuthConfig.d.ts.map +1 -0
- package/dist/utils/detectAuthConfig.js +31 -0
- package/dist/utils/detectAuthConfig.js.map +1 -0
- package/dist/utils/session.d.ts +63 -0
- package/dist/utils/session.d.ts.map +1 -0
- package/dist/utils/session.js +65 -0
- package/dist/utils/session.js.map +1 -0
- package/package.json +22 -23
- package/dist/adapter/index.d.mts +0 -70
- package/dist/adapter/index.mjs +0 -366
- package/dist/adapter/index.mjs.map +0 -1
- package/dist/client.d.mts +0 -1
- package/dist/client.d.ts +0 -1
- package/dist/client.js +0 -12
- package/dist/client.js.map +0 -1
- package/dist/client.mjs +0 -3
- package/dist/client.mjs.map +0 -1
- package/dist/index.d.mts +0 -120
- package/dist/index.mjs +0 -603
- package/dist/index.mjs.map +0 -1
- package/dist/plugin/index.d.mts +0 -78
- package/dist/plugin/index.mjs +0 -82
- package/dist/plugin/index.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ Better Auth adapter and plugins for Payload CMS. Enables seamless integration be
|
|
|
9
9
|
- [Installation](#installation)
|
|
10
10
|
- [Quick Start](#quick-start)
|
|
11
11
|
- [API Reference](#api-reference)
|
|
12
|
-
- [
|
|
12
|
+
- [Customization](#customization)
|
|
13
13
|
- [Plugin Compatibility](#plugin-compatibility)
|
|
14
14
|
- [License](#license)
|
|
15
15
|
|
|
@@ -19,12 +19,12 @@ Better Auth adapter and plugins for Payload CMS. Enables seamless integration be
|
|
|
19
19
|
|
|
20
20
|
### Requirements
|
|
21
21
|
|
|
22
|
-
| Dependency | Version |
|
|
23
|
-
|
|
24
|
-
| `better-auth` | >= 1.
|
|
25
|
-
| `payload` | >= 3.0.0 |
|
|
26
|
-
| `next` | >= 15.
|
|
27
|
-
| `react` | >= 18.0.0 |
|
|
22
|
+
| Dependency | Version | Latest |
|
|
23
|
+
|------------|---------|--------|
|
|
24
|
+
| `better-auth` | >= 1.4.0 | 1.4.11 |
|
|
25
|
+
| `payload` | >= 3.0.0 | 3.70.0 |
|
|
26
|
+
| `next` | >= 15.5.9 | 16.1.1 |
|
|
27
|
+
| `react` | >= 18.0.0 | 19.2.3 |
|
|
28
28
|
|
|
29
29
|
### Install
|
|
30
30
|
|
|
@@ -66,72 +66,7 @@ export const collectionSlugs = {
|
|
|
66
66
|
} as const
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
-
### Step 2: Create
|
|
70
|
-
|
|
71
|
-
```ts
|
|
72
|
-
// src/lib/auth/index.ts
|
|
73
|
-
import { betterAuth } from 'better-auth'
|
|
74
|
-
import type { BasePayload } from 'payload'
|
|
75
|
-
import { payloadAdapter } from '@delmaredigital/payload-better-auth'
|
|
76
|
-
import { betterAuthOptions, collectionSlugs } from './config'
|
|
77
|
-
|
|
78
|
-
export function createAuth(payload: BasePayload) {
|
|
79
|
-
return betterAuth({
|
|
80
|
-
...betterAuthOptions,
|
|
81
|
-
database: payloadAdapter({
|
|
82
|
-
payloadClient: payload,
|
|
83
|
-
adapterConfig: {
|
|
84
|
-
collections: collectionSlugs,
|
|
85
|
-
enableDebugLogs: process.env.NODE_ENV === 'development',
|
|
86
|
-
idType: 'number', // Use Payload's default SERIAL IDs
|
|
87
|
-
},
|
|
88
|
-
}),
|
|
89
|
-
// Use serial/integer IDs (Payload default) instead of UUID
|
|
90
|
-
advanced: {
|
|
91
|
-
database: {
|
|
92
|
-
generateId: 'serial',
|
|
93
|
-
},
|
|
94
|
-
},
|
|
95
|
-
secret: process.env.BETTER_AUTH_SECRET,
|
|
96
|
-
trustedOrigins: [process.env.NEXT_PUBLIC_APP_URL || ''],
|
|
97
|
-
})
|
|
98
|
-
}
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
### Step 3: Configure Payload
|
|
102
|
-
|
|
103
|
-
```ts
|
|
104
|
-
// src/payload.config.ts
|
|
105
|
-
import { buildConfig } from 'payload'
|
|
106
|
-
import {
|
|
107
|
-
betterAuthCollections,
|
|
108
|
-
createBetterAuthPlugin,
|
|
109
|
-
} from '@delmaredigital/payload-better-auth'
|
|
110
|
-
import { betterAuthOptions } from './lib/auth/config'
|
|
111
|
-
import { createAuth } from './lib/auth'
|
|
112
|
-
import { Users } from './collections/Users'
|
|
113
|
-
|
|
114
|
-
export default buildConfig({
|
|
115
|
-
collections: [Users /* ... other collections */],
|
|
116
|
-
plugins: [
|
|
117
|
-
// Auto-generate sessions, accounts, verifications collections
|
|
118
|
-
betterAuthCollections({
|
|
119
|
-
betterAuthOptions,
|
|
120
|
-
skipCollections: ['user'], // We define Users ourselves
|
|
121
|
-
}),
|
|
122
|
-
// Initialize Better Auth in Payload's lifecycle
|
|
123
|
-
createBetterAuthPlugin({
|
|
124
|
-
createAuth,
|
|
125
|
-
}),
|
|
126
|
-
],
|
|
127
|
-
db: postgresAdapter({
|
|
128
|
-
pool: { connectionString: process.env.DATABASE_URL },
|
|
129
|
-
// Use Payload defaults - Better Auth adapter handles ID conversion
|
|
130
|
-
}),
|
|
131
|
-
})
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
### Step 4: Create Your Users Collection
|
|
69
|
+
### Step 2: Create Your Users Collection
|
|
135
70
|
|
|
136
71
|
```ts
|
|
137
72
|
// src/collections/Users.ts
|
|
@@ -170,27 +105,62 @@ export const Users: CollectionConfig = {
|
|
|
170
105
|
}
|
|
171
106
|
```
|
|
172
107
|
|
|
173
|
-
### Step
|
|
108
|
+
### Step 3: Configure Payload
|
|
174
109
|
|
|
175
110
|
```ts
|
|
176
|
-
// src/
|
|
177
|
-
import {
|
|
178
|
-
import
|
|
179
|
-
import
|
|
180
|
-
import
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
111
|
+
// src/payload.config.ts
|
|
112
|
+
import { buildConfig } from 'payload'
|
|
113
|
+
import { postgresAdapter } from '@payloadcms/db-postgres'
|
|
114
|
+
import { betterAuth } from 'better-auth'
|
|
115
|
+
import {
|
|
116
|
+
betterAuthCollections,
|
|
117
|
+
createBetterAuthPlugin,
|
|
118
|
+
payloadAdapter,
|
|
119
|
+
} from '@delmaredigital/payload-better-auth'
|
|
120
|
+
import { betterAuthOptions, collectionSlugs } from './lib/auth/config'
|
|
121
|
+
import { Users } from './collections/Users'
|
|
186
122
|
|
|
187
|
-
export
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
123
|
+
export default buildConfig({
|
|
124
|
+
collections: [Users /* ...other collections */],
|
|
125
|
+
plugins: [
|
|
126
|
+
// Auto-generate sessions, accounts, verifications collections
|
|
127
|
+
betterAuthCollections({
|
|
128
|
+
betterAuthOptions,
|
|
129
|
+
skipCollections: ['user'], // We define Users ourselves
|
|
130
|
+
}),
|
|
131
|
+
// Initialize Better Auth with auto-injected endpoints and admin components
|
|
132
|
+
createBetterAuthPlugin({
|
|
133
|
+
createAuth: (payload) =>
|
|
134
|
+
betterAuth({
|
|
135
|
+
...betterAuthOptions,
|
|
136
|
+
database: payloadAdapter({
|
|
137
|
+
payloadClient: payload,
|
|
138
|
+
adapterConfig: {
|
|
139
|
+
collections: collectionSlugs,
|
|
140
|
+
enableDebugLogs: process.env.NODE_ENV === 'development',
|
|
141
|
+
idType: 'number', // Use Payload's default SERIAL IDs
|
|
142
|
+
},
|
|
143
|
+
}),
|
|
144
|
+
advanced: {
|
|
145
|
+
database: {
|
|
146
|
+
generateId: 'serial',
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
secret: process.env.BETTER_AUTH_SECRET,
|
|
150
|
+
trustedOrigins: [process.env.NEXT_PUBLIC_APP_URL || ''],
|
|
151
|
+
}),
|
|
152
|
+
}),
|
|
153
|
+
],
|
|
154
|
+
admin: {
|
|
155
|
+
user: 'users',
|
|
156
|
+
},
|
|
157
|
+
db: postgresAdapter({
|
|
158
|
+
pool: { connectionString: process.env.DATABASE_URL },
|
|
159
|
+
}),
|
|
160
|
+
})
|
|
191
161
|
```
|
|
192
162
|
|
|
193
|
-
### Step
|
|
163
|
+
### Step 4: Client-Side Auth
|
|
194
164
|
|
|
195
165
|
```ts
|
|
196
166
|
// src/lib/auth/client.ts
|
|
@@ -208,7 +178,7 @@ export const authClient = createAuthClient({
|
|
|
208
178
|
export const { useSession, signIn, signUp, signOut } = authClient
|
|
209
179
|
```
|
|
210
180
|
|
|
211
|
-
### Step
|
|
181
|
+
### Step 5: Server-Side Session Access
|
|
212
182
|
|
|
213
183
|
```ts
|
|
214
184
|
// In a server component or API route
|
|
@@ -229,6 +199,11 @@ export default async function Dashboard() {
|
|
|
229
199
|
}
|
|
230
200
|
```
|
|
231
201
|
|
|
202
|
+
**That's it!** The plugin automatically:
|
|
203
|
+
- Registers auth API endpoints at `/api/auth/*`
|
|
204
|
+
- Injects logout button, login redirect, and login page components
|
|
205
|
+
- Handles session management via Better Auth
|
|
206
|
+
|
|
232
207
|
---
|
|
233
208
|
|
|
234
209
|
## API Reference
|
|
@@ -287,12 +262,30 @@ Payload plugin that initializes Better Auth during Payload's `onInit`.
|
|
|
287
262
|
```ts
|
|
288
263
|
createBetterAuthPlugin({
|
|
289
264
|
createAuth: (payload) => betterAuth({ ... }),
|
|
265
|
+
authBasePath: '/auth',
|
|
266
|
+
autoRegisterEndpoints: true,
|
|
267
|
+
autoInjectAdminComponents: true,
|
|
268
|
+
admin: {
|
|
269
|
+
login: { title: 'Admin Login' },
|
|
270
|
+
},
|
|
290
271
|
})
|
|
291
272
|
```
|
|
292
273
|
|
|
293
|
-
| Option | Type | Description |
|
|
294
|
-
|
|
295
|
-
| `createAuth` | `(payload: BasePayload) => Auth` | Factory function that creates the Better Auth instance |
|
|
274
|
+
| Option | Type | Default | Description |
|
|
275
|
+
|--------|------|---------|-------------|
|
|
276
|
+
| `createAuth` | `(payload: BasePayload) => Auth` | *required* | Factory function that creates the Better Auth instance |
|
|
277
|
+
| `authBasePath` | `string` | `'/auth'` | Base path for auth API endpoints |
|
|
278
|
+
| `autoRegisterEndpoints` | `boolean` | `true` | Auto-register auth API endpoints |
|
|
279
|
+
| `autoInjectAdminComponents` | `boolean` | `true` | Auto-inject admin components when `disableLocalStrategy` detected |
|
|
280
|
+
| `admin.disableLogoutButton` | `boolean` | `false` | Disable logout button injection |
|
|
281
|
+
| `admin.disableBeforeLogin` | `boolean` | `false` | Disable BeforeLogin redirect injection |
|
|
282
|
+
| `admin.disableLoginView` | `boolean` | `false` | Disable login view injection |
|
|
283
|
+
| `admin.login.title` | `string` | `'Login'` | Custom login page title |
|
|
284
|
+
| `admin.login.afterLoginPath` | `string` | `'/admin'` | Redirect path after successful login |
|
|
285
|
+
| `admin.login.requiredRole` | `string \| null` | `'admin'` | Required role for admin access. Set to `null` to disable role checking. |
|
|
286
|
+
| `admin.logoutButtonComponent` | `string` | - | Override logout button (import map format) |
|
|
287
|
+
| `admin.beforeLoginComponent` | `string` | - | Override BeforeLogin component |
|
|
288
|
+
| `admin.loginViewComponent` | `string` | - | Override login view component |
|
|
296
289
|
|
|
297
290
|
### `betterAuthStrategy(options?)`
|
|
298
291
|
|
|
@@ -328,189 +321,111 @@ const user = await getServerUser(payload, headersList)
|
|
|
328
321
|
|
|
329
322
|
---
|
|
330
323
|
|
|
331
|
-
##
|
|
332
|
-
|
|
333
|
-
When using `disableLocalStrategy: true` in your Users collection, you need custom admin authentication components since Payload's default login form won't work.
|
|
334
|
-
|
|
335
|
-
### Why Custom Components Are Needed
|
|
324
|
+
## Customization
|
|
336
325
|
|
|
337
|
-
|
|
338
|
-
- Payload's default login form is disabled
|
|
339
|
-
- Users must authenticate via Better Auth
|
|
340
|
-
- A custom login page is needed at `/admin/login`
|
|
341
|
-
- A custom logout button is needed to clear Better Auth sessions
|
|
326
|
+
### Role-Based Access Control
|
|
342
327
|
|
|
343
|
-
|
|
344
|
-
<summary><strong>Step 1: Create BeforeLogin Component</strong></summary>
|
|
345
|
-
|
|
346
|
-
This component redirects unauthenticated users from Payload's login to your custom login page:
|
|
328
|
+
By default, the login page checks that users have the `admin` role before allowing access to the admin panel. Users without the required role see an "Access Denied" message.
|
|
347
329
|
|
|
348
|
-
```
|
|
349
|
-
|
|
350
|
-
|
|
330
|
+
```ts
|
|
331
|
+
createBetterAuthPlugin({
|
|
332
|
+
createAuth,
|
|
333
|
+
admin: {
|
|
334
|
+
login: {
|
|
335
|
+
// Default: 'admin' - only users with role='admin' can access
|
|
336
|
+
requiredRole: 'admin',
|
|
351
337
|
|
|
352
|
-
|
|
353
|
-
|
|
338
|
+
// Use a different role name
|
|
339
|
+
requiredRole: 'editor',
|
|
354
340
|
|
|
355
|
-
|
|
356
|
-
|
|
341
|
+
// Disable role checking entirely
|
|
342
|
+
requiredRole: null,
|
|
343
|
+
},
|
|
344
|
+
},
|
|
345
|
+
})
|
|
346
|
+
```
|
|
357
347
|
|
|
358
|
-
|
|
359
|
-
router.replace('/admin/login')
|
|
360
|
-
}, [router])
|
|
348
|
+
**For complex RBAC** (multiple roles, permissions, etc.), disable the login view and create your own:
|
|
361
349
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
350
|
+
```ts
|
|
351
|
+
createBetterAuthPlugin({
|
|
352
|
+
createAuth,
|
|
353
|
+
admin: {
|
|
354
|
+
disableLoginView: true,
|
|
355
|
+
loginViewComponent: '@/components/admin/CustomLoginWithRBAC',
|
|
356
|
+
},
|
|
357
|
+
})
|
|
368
358
|
```
|
|
369
|
-
</details>
|
|
370
359
|
|
|
371
|
-
|
|
372
|
-
<summary><strong>Step 2: Create Custom Logout Button</strong></summary>
|
|
373
|
-
|
|
374
|
-
**IMPORTANT**: The logout button must only trigger logout **on click**, not on mount. Triggering logout on mount would cause an infinite redirect loop since this component is rendered in the admin panel header.
|
|
360
|
+
You can use the built-in `LoginView` as a starting point:
|
|
375
361
|
|
|
376
362
|
```tsx
|
|
377
|
-
// src/components/admin/
|
|
363
|
+
// src/components/admin/CustomLoginWithRBAC.tsx
|
|
378
364
|
'use client'
|
|
379
365
|
|
|
380
|
-
import {
|
|
381
|
-
import { useRouter } from 'next/navigation'
|
|
382
|
-
import { signOut } from '@/lib/auth/client'
|
|
383
|
-
|
|
384
|
-
export default function Logout() {
|
|
385
|
-
const router = useRouter()
|
|
386
|
-
const [isLoading, setIsLoading] = useState(false)
|
|
387
|
-
|
|
388
|
-
async function handleLogout() {
|
|
389
|
-
if (isLoading) return
|
|
390
|
-
setIsLoading(true)
|
|
391
|
-
|
|
392
|
-
try {
|
|
393
|
-
await signOut()
|
|
394
|
-
router.push('/admin/login')
|
|
395
|
-
} catch (error) {
|
|
396
|
-
console.error('Logout error:', error)
|
|
397
|
-
setIsLoading(false)
|
|
398
|
-
}
|
|
399
|
-
}
|
|
366
|
+
import { LoginView } from '@delmaredigital/payload-better-auth/components'
|
|
400
367
|
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
type="button"
|
|
406
|
-
className="btn btn--style-secondary btn--icon-style-without-border btn--size-small btn--withoutPopup"
|
|
407
|
-
>
|
|
408
|
-
{isLoading ? 'Logging out...' : 'Log out'}
|
|
409
|
-
</button>
|
|
410
|
-
)
|
|
368
|
+
// Option 1: Wrap and extend the built-in component
|
|
369
|
+
export default function CustomLoginWithRBAC() {
|
|
370
|
+
// Add your custom RBAC logic here
|
|
371
|
+
return <LoginView requiredRole={null} /> // Disable built-in role check
|
|
411
372
|
}
|
|
412
|
-
```
|
|
413
|
-
</details>
|
|
414
373
|
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
```tsx
|
|
419
|
-
// src/app/(frontend)/admin/login/page.tsx
|
|
420
|
-
'use client'
|
|
374
|
+
// Option 2: Copy the LoginView source code from the package and customize fully
|
|
375
|
+
// See: node_modules/@delmaredigital/payload-better-auth/dist/components/LoginView.js
|
|
376
|
+
```
|
|
421
377
|
|
|
422
|
-
|
|
423
|
-
import { useRouter } from 'next/navigation'
|
|
424
|
-
import { useSession, signIn } from '@/lib/auth/client'
|
|
425
|
-
|
|
426
|
-
export default function AdminLoginPage() {
|
|
427
|
-
const { data: session, isPending } = useSession()
|
|
428
|
-
const router = useRouter()
|
|
429
|
-
const [email, setEmail] = useState('')
|
|
430
|
-
const [password, setPassword] = useState('')
|
|
431
|
-
const [error, setError] = useState<string | null>(null)
|
|
432
|
-
const [isLoading, setIsLoading] = useState(false)
|
|
433
|
-
|
|
434
|
-
useEffect(() => {
|
|
435
|
-
if (session?.user) {
|
|
436
|
-
const user = session.user as { role?: string }
|
|
437
|
-
if (user.role === 'admin') {
|
|
438
|
-
router.push('/admin')
|
|
439
|
-
} else {
|
|
440
|
-
setError('Access denied. Admin role required.')
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
}, [session, router])
|
|
444
|
-
|
|
445
|
-
async function handleSubmit(e: FormEvent) {
|
|
446
|
-
e.preventDefault()
|
|
447
|
-
setError(null)
|
|
448
|
-
setIsLoading(true)
|
|
449
|
-
|
|
450
|
-
try {
|
|
451
|
-
const result = await signIn.email({ email, password })
|
|
452
|
-
if (result.error) {
|
|
453
|
-
setError(result.error.message || 'Invalid credentials')
|
|
454
|
-
setIsLoading(false)
|
|
455
|
-
return
|
|
456
|
-
}
|
|
457
|
-
router.refresh()
|
|
458
|
-
} catch {
|
|
459
|
-
setError('An unexpected error occurred')
|
|
460
|
-
setIsLoading(false)
|
|
461
|
-
}
|
|
462
|
-
}
|
|
378
|
+
### Disabling Auto-Injection
|
|
463
379
|
|
|
464
|
-
|
|
465
|
-
return <div>Loading...</div>
|
|
466
|
-
}
|
|
380
|
+
If you prefer to handle API routes or admin components manually:
|
|
467
381
|
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
onChange={(e) => setEmail(e.target.value)}
|
|
475
|
-
placeholder="Email"
|
|
476
|
-
required
|
|
477
|
-
/>
|
|
478
|
-
<input
|
|
479
|
-
type="password"
|
|
480
|
-
value={password}
|
|
481
|
-
onChange={(e) => setPassword(e.target.value)}
|
|
482
|
-
placeholder="Password"
|
|
483
|
-
required
|
|
484
|
-
/>
|
|
485
|
-
{error && <div style={{ color: 'red' }}>{error}</div>}
|
|
486
|
-
<button type="submit" disabled={isLoading}>
|
|
487
|
-
{isLoading ? 'Signing in...' : 'Sign in'}
|
|
488
|
-
</button>
|
|
489
|
-
</form>
|
|
490
|
-
)
|
|
491
|
-
}
|
|
382
|
+
```ts
|
|
383
|
+
createBetterAuthPlugin({
|
|
384
|
+
createAuth,
|
|
385
|
+
autoRegisterEndpoints: false, // Handle API route yourself
|
|
386
|
+
autoInjectAdminComponents: false, // Handle admin components yourself
|
|
387
|
+
})
|
|
492
388
|
```
|
|
493
|
-
</details>
|
|
494
389
|
|
|
495
|
-
|
|
496
|
-
|
|
390
|
+
### Custom Admin Components
|
|
391
|
+
|
|
392
|
+
Override specific admin components while keeping others auto-injected:
|
|
497
393
|
|
|
498
394
|
```ts
|
|
499
|
-
|
|
500
|
-
|
|
395
|
+
createBetterAuthPlugin({
|
|
396
|
+
createAuth,
|
|
501
397
|
admin: {
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
},
|
|
398
|
+
// Use custom components (import map format)
|
|
399
|
+
loginViewComponent: '@/components/admin/CustomLogin',
|
|
400
|
+
logoutButtonComponent: '@/components/admin/CustomLogout',
|
|
401
|
+
|
|
402
|
+
// Or disable specific components
|
|
403
|
+
disableBeforeLogin: true,
|
|
509
404
|
},
|
|
510
|
-
// ... rest of config
|
|
511
405
|
})
|
|
512
406
|
```
|
|
513
|
-
|
|
407
|
+
|
|
408
|
+
### Manual API Route (Advanced)
|
|
409
|
+
|
|
410
|
+
If you disable `autoRegisterEndpoints`, create your own route:
|
|
411
|
+
|
|
412
|
+
```ts
|
|
413
|
+
// src/app/api/auth/[...all]/route.ts
|
|
414
|
+
import { getPayload } from 'payload'
|
|
415
|
+
import config from '@payload-config'
|
|
416
|
+
import type { NextRequest } from 'next/server'
|
|
417
|
+
import type { PayloadWithAuth } from '@delmaredigital/payload-better-auth'
|
|
418
|
+
|
|
419
|
+
export async function GET(request: NextRequest) {
|
|
420
|
+
const payload = (await getPayload({ config })) as PayloadWithAuth
|
|
421
|
+
return payload.betterAuth.handler(request)
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
export async function POST(request: NextRequest) {
|
|
425
|
+
const payload = (await getPayload({ config })) as PayloadWithAuth
|
|
426
|
+
return payload.betterAuth.handler(request)
|
|
427
|
+
}
|
|
428
|
+
```
|
|
514
429
|
|
|
515
430
|
---
|
|
516
431
|
|
|
@@ -712,4 +627,4 @@ The auto-generated collections create relationship fields like `user` (from `use
|
|
|
712
627
|
|
|
713
628
|
## License
|
|
714
629
|
|
|
715
|
-
MIT
|
|
630
|
+
MIT
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-generate Payload collections from Better Auth schema
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
import type { CollectionConfig, Plugin } from 'payload';
|
|
7
|
+
import type { BetterAuthOptions } from 'better-auth';
|
|
8
|
+
export type BetterAuthCollectionsOptions = {
|
|
9
|
+
/**
|
|
10
|
+
* Better Auth options. Pass the same options you use for betterAuth().
|
|
11
|
+
* The plugin reads the schema to generate collections.
|
|
12
|
+
*/
|
|
13
|
+
betterAuthOptions?: BetterAuthOptions;
|
|
14
|
+
/**
|
|
15
|
+
* Override collection slugs (e.g., { user: 'users', session: 'sessions' })
|
|
16
|
+
*/
|
|
17
|
+
slugOverrides?: Record<string, string>;
|
|
18
|
+
/**
|
|
19
|
+
* Collections to skip (they already exist in your config)
|
|
20
|
+
* Default: ['user'] - assumes you have a Users collection
|
|
21
|
+
*/
|
|
22
|
+
skipCollections?: string[];
|
|
23
|
+
/**
|
|
24
|
+
* Admin group name for generated collections
|
|
25
|
+
* Default: 'Auth'
|
|
26
|
+
*/
|
|
27
|
+
adminGroup?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Custom access control for generated collections.
|
|
30
|
+
* By default, only admins can read/delete, and create/update are disabled.
|
|
31
|
+
*/
|
|
32
|
+
access?: CollectionConfig['access'];
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Payload plugin that auto-generates collections from Better Auth schema.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* import { betterAuthCollections } from '@delmare/payload-better-auth'
|
|
40
|
+
*
|
|
41
|
+
* export default buildConfig({
|
|
42
|
+
* plugins: [
|
|
43
|
+
* betterAuthCollections({
|
|
44
|
+
* betterAuthOptions: { ... },
|
|
45
|
+
* skipCollections: ['user'], // Define Users yourself
|
|
46
|
+
* }),
|
|
47
|
+
* ],
|
|
48
|
+
* })
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function betterAuthCollections(options?: BetterAuthCollectionsOptions): Plugin;
|
|
52
|
+
//# sourceMappingURL=collections.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collections.d.ts","sourceRoot":"","sources":["../../src/adapter/collections.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAU,gBAAgB,EAAS,MAAM,EAAE,MAAM,SAAS,CAAA;AACtE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAGpD,MAAM,MAAM,4BAA4B,GAAG;IACzC;;;OAGG;IACH,iBAAiB,CAAC,EAAE,iBAAiB,CAAA;IAErC;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAEtC;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,EAAE,CAAA;IAE1B;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;IAEnB;;;OAGG;IACH,MAAM,CAAC,EAAE,gBAAgB,CAAC,QAAQ,CAAC,CAAA;CACpC,CAAA;AAiID;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,GAAE,4BAAiC,GACzC,MAAM,CAkDR"}
|