@delmaredigital/payload-better-auth 0.1.4 → 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/LICENSE +21 -73
- package/README.md +165 -290
- 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/LICENSE
CHANGED
|
@@ -1,73 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
## Changes and New Works License
|
|
24
|
-
|
|
25
|
-
The licensor grants you an additional copyright license to make changes and new works based on the software for any permitted purpose.
|
|
26
|
-
|
|
27
|
-
## Patent License
|
|
28
|
-
|
|
29
|
-
The licensor grants you a patent license for the software that covers patent claims the licensor can license, or becomes able to license, that you would infringe by using the software.
|
|
30
|
-
|
|
31
|
-
## Noncommercial Purposes
|
|
32
|
-
|
|
33
|
-
Any noncommercial purpose is a permitted purpose.
|
|
34
|
-
|
|
35
|
-
## Personal Uses
|
|
36
|
-
|
|
37
|
-
Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, is use for a permitted purpose.
|
|
38
|
-
|
|
39
|
-
## Noncommercial Organizations
|
|
40
|
-
|
|
41
|
-
Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution is use for a permitted purpose regardless of the source of funding or obligations resulting from the funding.
|
|
42
|
-
|
|
43
|
-
## Fair Use
|
|
44
|
-
|
|
45
|
-
You may have "fair use" rights for the software under the law. These terms do not limit them.
|
|
46
|
-
|
|
47
|
-
## No Other Rights
|
|
48
|
-
|
|
49
|
-
These terms do not allow you to sublicense or transfer any of your licenses to anyone else, or prevent the licensor from granting licenses to anyone else. These terms do not imply any other licenses.
|
|
50
|
-
|
|
51
|
-
## Patent Defense
|
|
52
|
-
|
|
53
|
-
If you make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
|
|
54
|
-
|
|
55
|
-
## Violations
|
|
56
|
-
|
|
57
|
-
The first time you are notified in writing that you have violated any of these terms, or done anything with the software not covered by your licenses, your licenses can nonetheless continue if you come into full compliance with these terms, and take practical steps to correct past violations, within 32 days of receiving notice. Otherwise, all your licenses end immediately.
|
|
58
|
-
|
|
59
|
-
## No Liability
|
|
60
|
-
|
|
61
|
-
***As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.***
|
|
62
|
-
|
|
63
|
-
## Definitions
|
|
64
|
-
|
|
65
|
-
The **licensor** is the individual or entity offering these terms, and the **software** is the software the licensor makes available under these terms.
|
|
66
|
-
|
|
67
|
-
**You** refers to the individual or entity agreeing to these terms.
|
|
68
|
-
|
|
69
|
-
**Your company** is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. **Control** means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
|
|
70
|
-
|
|
71
|
-
**Your licenses** are all the licenses granted to you for the software under these terms.
|
|
72
|
-
|
|
73
|
-
**Use** means anything you do with the software requiring one of your licenses.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Delmare Digital
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
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
|
|
|
@@ -32,25 +32,6 @@ Better Auth adapter and plugins for Payload CMS. Enables seamless integration be
|
|
|
32
32
|
pnpm add @delmaredigital/payload-better-auth better-auth
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
Or install from GitHub:
|
|
36
|
-
|
|
37
|
-
```bash
|
|
38
|
-
pnpm add github:delmaredigital/payload-better-auth
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
### Local Development
|
|
42
|
-
|
|
43
|
-
For local development with hot reloading:
|
|
44
|
-
|
|
45
|
-
```bash
|
|
46
|
-
# In the package directory
|
|
47
|
-
cd path/to/payload-better-auth
|
|
48
|
-
pnpm link --global
|
|
49
|
-
|
|
50
|
-
# In your project
|
|
51
|
-
pnpm link --global @delmaredigital/payload-better-auth
|
|
52
|
-
```
|
|
53
|
-
|
|
54
35
|
---
|
|
55
36
|
|
|
56
37
|
## Quick Start
|
|
@@ -85,72 +66,7 @@ export const collectionSlugs = {
|
|
|
85
66
|
} as const
|
|
86
67
|
```
|
|
87
68
|
|
|
88
|
-
### Step 2: Create
|
|
89
|
-
|
|
90
|
-
```ts
|
|
91
|
-
// src/lib/auth/index.ts
|
|
92
|
-
import { betterAuth } from 'better-auth'
|
|
93
|
-
import type { BasePayload } from 'payload'
|
|
94
|
-
import { payloadAdapter } from '@delmaredigital/payload-better-auth'
|
|
95
|
-
import { betterAuthOptions, collectionSlugs } from './config'
|
|
96
|
-
|
|
97
|
-
export function createAuth(payload: BasePayload) {
|
|
98
|
-
return betterAuth({
|
|
99
|
-
...betterAuthOptions,
|
|
100
|
-
database: payloadAdapter({
|
|
101
|
-
payloadClient: payload,
|
|
102
|
-
adapterConfig: {
|
|
103
|
-
collections: collectionSlugs,
|
|
104
|
-
enableDebugLogs: process.env.NODE_ENV === 'development',
|
|
105
|
-
idType: 'number', // Use Payload's default SERIAL IDs
|
|
106
|
-
},
|
|
107
|
-
}),
|
|
108
|
-
// Use serial/integer IDs (Payload default) instead of UUID
|
|
109
|
-
advanced: {
|
|
110
|
-
database: {
|
|
111
|
-
generateId: 'serial',
|
|
112
|
-
},
|
|
113
|
-
},
|
|
114
|
-
secret: process.env.BETTER_AUTH_SECRET,
|
|
115
|
-
trustedOrigins: [process.env.NEXT_PUBLIC_APP_URL || ''],
|
|
116
|
-
})
|
|
117
|
-
}
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
### Step 3: Configure Payload
|
|
121
|
-
|
|
122
|
-
```ts
|
|
123
|
-
// src/payload.config.ts
|
|
124
|
-
import { buildConfig } from 'payload'
|
|
125
|
-
import {
|
|
126
|
-
betterAuthCollections,
|
|
127
|
-
createBetterAuthPlugin,
|
|
128
|
-
} from '@delmaredigital/payload-better-auth'
|
|
129
|
-
import { betterAuthOptions } from './lib/auth/config'
|
|
130
|
-
import { createAuth } from './lib/auth'
|
|
131
|
-
import { Users } from './collections/Users'
|
|
132
|
-
|
|
133
|
-
export default buildConfig({
|
|
134
|
-
collections: [Users /* ... other collections */],
|
|
135
|
-
plugins: [
|
|
136
|
-
// Auto-generate sessions, accounts, verifications collections
|
|
137
|
-
betterAuthCollections({
|
|
138
|
-
betterAuthOptions,
|
|
139
|
-
skipCollections: ['user'], // We define Users ourselves
|
|
140
|
-
}),
|
|
141
|
-
// Initialize Better Auth in Payload's lifecycle
|
|
142
|
-
createBetterAuthPlugin({
|
|
143
|
-
createAuth,
|
|
144
|
-
}),
|
|
145
|
-
],
|
|
146
|
-
db: postgresAdapter({
|
|
147
|
-
pool: { connectionString: process.env.DATABASE_URL },
|
|
148
|
-
// Use Payload defaults - Better Auth adapter handles ID conversion
|
|
149
|
-
}),
|
|
150
|
-
})
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
### Step 4: Create Your Users Collection
|
|
69
|
+
### Step 2: Create Your Users Collection
|
|
154
70
|
|
|
155
71
|
```ts
|
|
156
72
|
// src/collections/Users.ts
|
|
@@ -189,27 +105,62 @@ export const Users: CollectionConfig = {
|
|
|
189
105
|
}
|
|
190
106
|
```
|
|
191
107
|
|
|
192
|
-
### Step
|
|
108
|
+
### Step 3: Configure Payload
|
|
193
109
|
|
|
194
110
|
```ts
|
|
195
|
-
// src/
|
|
196
|
-
import {
|
|
197
|
-
import
|
|
198
|
-
import
|
|
199
|
-
import
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
}
|
|
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'
|
|
205
122
|
|
|
206
|
-
export
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
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
|
+
})
|
|
210
161
|
```
|
|
211
162
|
|
|
212
|
-
### Step
|
|
163
|
+
### Step 4: Client-Side Auth
|
|
213
164
|
|
|
214
165
|
```ts
|
|
215
166
|
// src/lib/auth/client.ts
|
|
@@ -227,7 +178,7 @@ export const authClient = createAuthClient({
|
|
|
227
178
|
export const { useSession, signIn, signUp, signOut } = authClient
|
|
228
179
|
```
|
|
229
180
|
|
|
230
|
-
### Step
|
|
181
|
+
### Step 5: Server-Side Session Access
|
|
231
182
|
|
|
232
183
|
```ts
|
|
233
184
|
// In a server component or API route
|
|
@@ -248,6 +199,11 @@ export default async function Dashboard() {
|
|
|
248
199
|
}
|
|
249
200
|
```
|
|
250
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
|
+
|
|
251
207
|
---
|
|
252
208
|
|
|
253
209
|
## API Reference
|
|
@@ -306,12 +262,30 @@ Payload plugin that initializes Better Auth during Payload's `onInit`.
|
|
|
306
262
|
```ts
|
|
307
263
|
createBetterAuthPlugin({
|
|
308
264
|
createAuth: (payload) => betterAuth({ ... }),
|
|
265
|
+
authBasePath: '/auth',
|
|
266
|
+
autoRegisterEndpoints: true,
|
|
267
|
+
autoInjectAdminComponents: true,
|
|
268
|
+
admin: {
|
|
269
|
+
login: { title: 'Admin Login' },
|
|
270
|
+
},
|
|
309
271
|
})
|
|
310
272
|
```
|
|
311
273
|
|
|
312
|
-
| Option | Type | Description |
|
|
313
|
-
|
|
314
|
-
| `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 |
|
|
315
289
|
|
|
316
290
|
### `betterAuthStrategy(options?)`
|
|
317
291
|
|
|
@@ -347,189 +321,111 @@ const user = await getServerUser(payload, headersList)
|
|
|
347
321
|
|
|
348
322
|
---
|
|
349
323
|
|
|
350
|
-
##
|
|
351
|
-
|
|
352
|
-
When using `disableLocalStrategy: true` in your Users collection, you need custom admin authentication components since Payload's default login form won't work.
|
|
353
|
-
|
|
354
|
-
### Why Custom Components Are Needed
|
|
324
|
+
## Customization
|
|
355
325
|
|
|
356
|
-
|
|
357
|
-
- Payload's default login form is disabled
|
|
358
|
-
- Users must authenticate via Better Auth
|
|
359
|
-
- A custom login page is needed at `/admin/login`
|
|
360
|
-
- A custom logout button is needed to clear Better Auth sessions
|
|
326
|
+
### Role-Based Access Control
|
|
361
327
|
|
|
362
|
-
|
|
363
|
-
<summary><strong>Step 1: Create BeforeLogin Component</strong></summary>
|
|
364
|
-
|
|
365
|
-
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.
|
|
366
329
|
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
|
|
330
|
+
```ts
|
|
331
|
+
createBetterAuthPlugin({
|
|
332
|
+
createAuth,
|
|
333
|
+
admin: {
|
|
334
|
+
login: {
|
|
335
|
+
// Default: 'admin' - only users with role='admin' can access
|
|
336
|
+
requiredRole: 'admin',
|
|
370
337
|
|
|
371
|
-
|
|
372
|
-
|
|
338
|
+
// Use a different role name
|
|
339
|
+
requiredRole: 'editor',
|
|
373
340
|
|
|
374
|
-
|
|
375
|
-
|
|
341
|
+
// Disable role checking entirely
|
|
342
|
+
requiredRole: null,
|
|
343
|
+
},
|
|
344
|
+
},
|
|
345
|
+
})
|
|
346
|
+
```
|
|
376
347
|
|
|
377
|
-
|
|
378
|
-
router.replace('/admin/login')
|
|
379
|
-
}, [router])
|
|
348
|
+
**For complex RBAC** (multiple roles, permissions, etc.), disable the login view and create your own:
|
|
380
349
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
350
|
+
```ts
|
|
351
|
+
createBetterAuthPlugin({
|
|
352
|
+
createAuth,
|
|
353
|
+
admin: {
|
|
354
|
+
disableLoginView: true,
|
|
355
|
+
loginViewComponent: '@/components/admin/CustomLoginWithRBAC',
|
|
356
|
+
},
|
|
357
|
+
})
|
|
387
358
|
```
|
|
388
|
-
</details>
|
|
389
|
-
|
|
390
|
-
<details>
|
|
391
|
-
<summary><strong>Step 2: Create Custom Logout Button</strong></summary>
|
|
392
359
|
|
|
393
|
-
|
|
360
|
+
You can use the built-in `LoginView` as a starting point:
|
|
394
361
|
|
|
395
362
|
```tsx
|
|
396
|
-
// src/components/admin/
|
|
363
|
+
// src/components/admin/CustomLoginWithRBAC.tsx
|
|
397
364
|
'use client'
|
|
398
365
|
|
|
399
|
-
import {
|
|
400
|
-
import { useRouter } from 'next/navigation'
|
|
401
|
-
import { signOut } from '@/lib/auth/client'
|
|
402
|
-
|
|
403
|
-
export default function Logout() {
|
|
404
|
-
const router = useRouter()
|
|
405
|
-
const [isLoading, setIsLoading] = useState(false)
|
|
406
|
-
|
|
407
|
-
async function handleLogout() {
|
|
408
|
-
if (isLoading) return
|
|
409
|
-
setIsLoading(true)
|
|
410
|
-
|
|
411
|
-
try {
|
|
412
|
-
await signOut()
|
|
413
|
-
router.push('/admin/login')
|
|
414
|
-
} catch (error) {
|
|
415
|
-
console.error('Logout error:', error)
|
|
416
|
-
setIsLoading(false)
|
|
417
|
-
}
|
|
418
|
-
}
|
|
366
|
+
import { LoginView } from '@delmaredigital/payload-better-auth/components'
|
|
419
367
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
type="button"
|
|
425
|
-
className="btn btn--style-secondary btn--icon-style-without-border btn--size-small btn--withoutPopup"
|
|
426
|
-
>
|
|
427
|
-
{isLoading ? 'Logging out...' : 'Log out'}
|
|
428
|
-
</button>
|
|
429
|
-
)
|
|
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
|
|
430
372
|
}
|
|
431
|
-
```
|
|
432
|
-
</details>
|
|
433
|
-
|
|
434
|
-
<details>
|
|
435
|
-
<summary><strong>Step 3: Create Admin Login Page</strong></summary>
|
|
436
373
|
|
|
437
|
-
|
|
438
|
-
//
|
|
439
|
-
|
|
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
|
+
```
|
|
440
377
|
|
|
441
|
-
|
|
442
|
-
import { useRouter } from 'next/navigation'
|
|
443
|
-
import { useSession, signIn } from '@/lib/auth/client'
|
|
444
|
-
|
|
445
|
-
export default function AdminLoginPage() {
|
|
446
|
-
const { data: session, isPending } = useSession()
|
|
447
|
-
const router = useRouter()
|
|
448
|
-
const [email, setEmail] = useState('')
|
|
449
|
-
const [password, setPassword] = useState('')
|
|
450
|
-
const [error, setError] = useState<string | null>(null)
|
|
451
|
-
const [isLoading, setIsLoading] = useState(false)
|
|
452
|
-
|
|
453
|
-
useEffect(() => {
|
|
454
|
-
if (session?.user) {
|
|
455
|
-
const user = session.user as { role?: string }
|
|
456
|
-
if (user.role === 'admin') {
|
|
457
|
-
router.push('/admin')
|
|
458
|
-
} else {
|
|
459
|
-
setError('Access denied. Admin role required.')
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
}, [session, router])
|
|
463
|
-
|
|
464
|
-
async function handleSubmit(e: FormEvent) {
|
|
465
|
-
e.preventDefault()
|
|
466
|
-
setError(null)
|
|
467
|
-
setIsLoading(true)
|
|
468
|
-
|
|
469
|
-
try {
|
|
470
|
-
const result = await signIn.email({ email, password })
|
|
471
|
-
if (result.error) {
|
|
472
|
-
setError(result.error.message || 'Invalid credentials')
|
|
473
|
-
setIsLoading(false)
|
|
474
|
-
return
|
|
475
|
-
}
|
|
476
|
-
router.refresh()
|
|
477
|
-
} catch {
|
|
478
|
-
setError('An unexpected error occurred')
|
|
479
|
-
setIsLoading(false)
|
|
480
|
-
}
|
|
481
|
-
}
|
|
378
|
+
### Disabling Auto-Injection
|
|
482
379
|
|
|
483
|
-
|
|
484
|
-
return <div>Loading...</div>
|
|
485
|
-
}
|
|
380
|
+
If you prefer to handle API routes or admin components manually:
|
|
486
381
|
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
onChange={(e) => setEmail(e.target.value)}
|
|
494
|
-
placeholder="Email"
|
|
495
|
-
required
|
|
496
|
-
/>
|
|
497
|
-
<input
|
|
498
|
-
type="password"
|
|
499
|
-
value={password}
|
|
500
|
-
onChange={(e) => setPassword(e.target.value)}
|
|
501
|
-
placeholder="Password"
|
|
502
|
-
required
|
|
503
|
-
/>
|
|
504
|
-
{error && <div style={{ color: 'red' }}>{error}</div>}
|
|
505
|
-
<button type="submit" disabled={isLoading}>
|
|
506
|
-
{isLoading ? 'Signing in...' : 'Sign in'}
|
|
507
|
-
</button>
|
|
508
|
-
</form>
|
|
509
|
-
)
|
|
510
|
-
}
|
|
382
|
+
```ts
|
|
383
|
+
createBetterAuthPlugin({
|
|
384
|
+
createAuth,
|
|
385
|
+
autoRegisterEndpoints: false, // Handle API route yourself
|
|
386
|
+
autoInjectAdminComponents: false, // Handle admin components yourself
|
|
387
|
+
})
|
|
511
388
|
```
|
|
512
|
-
</details>
|
|
513
389
|
|
|
514
|
-
|
|
515
|
-
|
|
390
|
+
### Custom Admin Components
|
|
391
|
+
|
|
392
|
+
Override specific admin components while keeping others auto-injected:
|
|
516
393
|
|
|
517
394
|
```ts
|
|
518
|
-
|
|
519
|
-
|
|
395
|
+
createBetterAuthPlugin({
|
|
396
|
+
createAuth,
|
|
520
397
|
admin: {
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
},
|
|
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,
|
|
528
404
|
},
|
|
529
|
-
// ... rest of config
|
|
530
405
|
})
|
|
531
406
|
```
|
|
532
|
-
|
|
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
|
+
```
|
|
533
429
|
|
|
534
430
|
---
|
|
535
431
|
|
|
@@ -731,25 +627,4 @@ The auto-generated collections create relationship fields like `user` (from `use
|
|
|
731
627
|
|
|
732
628
|
## License
|
|
733
629
|
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
### What This Means
|
|
737
|
-
|
|
738
|
-
**✅ Free for:**
|
|
739
|
-
- Personal projects and hobby use
|
|
740
|
-
- Open source projects
|
|
741
|
-
- Educational and research purposes
|
|
742
|
-
- Evaluation and testing
|
|
743
|
-
- Nonprofit organizations
|
|
744
|
-
- Government institutions
|
|
745
|
-
|
|
746
|
-
**💼 Commercial use:**
|
|
747
|
-
Requires a separate commercial license. If you're using this in a commercial product or service, please contact us for licensing options.
|
|
748
|
-
|
|
749
|
-
**📧 Commercial Licensing:** [hello@delmaredigital.com](mailto:hello@delmaredigital.com)
|
|
750
|
-
|
|
751
|
-
---
|
|
752
|
-
|
|
753
|
-
## About
|
|
754
|
-
|
|
755
|
-
Built by [Delmare Digital](https://delmaredigital.com) — custom software solutions for growing businesses.
|
|
630
|
+
MIT
|