@delmaredigital/payload-better-auth 0.3.14 → 0.3.15
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 +2 -0
- package/dist/plugin/index.d.ts +11 -0
- package/dist/plugin/index.js +14 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -545,12 +545,14 @@ Payload auth strategy for Better Auth session validation.
|
|
|
545
545
|
```ts
|
|
546
546
|
betterAuthStrategy({
|
|
547
547
|
usersCollection: 'users',
|
|
548
|
+
idType: 'number', // default — coerces session field IDs for serial IDs
|
|
548
549
|
})
|
|
549
550
|
```
|
|
550
551
|
|
|
551
552
|
| Option | Type | Description |
|
|
552
553
|
|--------|------|-------------|
|
|
553
554
|
| `usersCollection` | `string` | The collection slug for users (default: `'users'`) |
|
|
555
|
+
| `idType` | `'number' \| 'text'` | Coerces string IDs in session fields (`activeOrganizationId`, etc.) to numbers. Defaults to `'number'` matching the adapter default. Set to `'text'` for UUID IDs. |
|
|
554
556
|
|
|
555
557
|
### `getServerSession<TUser>(payload, headers)`
|
|
556
558
|
|
package/dist/plugin/index.d.ts
CHANGED
|
@@ -178,6 +178,17 @@ export type BetterAuthStrategyOptions = {
|
|
|
178
178
|
* @default 'members'
|
|
179
179
|
*/
|
|
180
180
|
membersCollection?: string;
|
|
181
|
+
/**
|
|
182
|
+
* ID type strategy matching your adapter's `adapterConfig.idType`.
|
|
183
|
+
*
|
|
184
|
+
* When `'number'` (default), coerces string IDs in session fields
|
|
185
|
+
* (e.g., `activeOrganizationId`) to numbers before merging onto `req.user`.
|
|
186
|
+
* Better Auth always returns string IDs from `api.getSession()`, but Payload
|
|
187
|
+
* relationship fields expect numbers when using serial IDs.
|
|
188
|
+
*
|
|
189
|
+
* @default 'number'
|
|
190
|
+
*/
|
|
191
|
+
idType?: 'number' | 'text';
|
|
181
192
|
};
|
|
182
193
|
/**
|
|
183
194
|
* Payload auth strategy that uses Better Auth for authentication.
|
package/dist/plugin/index.js
CHANGED
|
@@ -459,7 +459,7 @@ let apiKeyScopesConfig = undefined;
|
|
|
459
459
|
* }
|
|
460
460
|
* ```
|
|
461
461
|
*/ export function betterAuthStrategy(options = {}) {
|
|
462
|
-
const { usersCollection = 'users', membersCollection = 'members' } = options;
|
|
462
|
+
const { usersCollection = 'users', membersCollection = 'members', idType = 'number' } = options;
|
|
463
463
|
return {
|
|
464
464
|
name: 'better-auth',
|
|
465
465
|
authenticate: async ({ payload, headers })=>{
|
|
@@ -498,6 +498,19 @@ let apiKeyScopesConfig = undefined;
|
|
|
498
498
|
// Extract session fields to merge onto user (e.g., activeOrganizationId from org plugin)
|
|
499
499
|
// Exclude fields that might conflict with user fields
|
|
500
500
|
const { id: _sessionId, userId: _userId, expiresAt: _expiresAt, token: _token, ...sessionFields } = sessionData.session || {};
|
|
501
|
+
// Coerce string IDs in session fields to numbers when using serial IDs.
|
|
502
|
+
// BA's api.getSession() always returns strings, but Payload relationship
|
|
503
|
+
// fields expect numbers for serial IDs.
|
|
504
|
+
if (idType === 'number') {
|
|
505
|
+
for (const [key, value] of Object.entries(sessionFields)){
|
|
506
|
+
if (typeof value !== 'string') continue;
|
|
507
|
+
if (key === 'id' || /(?:Id|_id)$/.test(key)) {
|
|
508
|
+
if (/^\d+$/.test(value)) {
|
|
509
|
+
sessionFields[key] = parseInt(value, 10);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
501
514
|
// If there's an active organization, fetch the user's role in that org
|
|
502
515
|
let organizationRole;
|
|
503
516
|
if (sessionFields.activeOrganizationId) {
|