Skip to content

Commit fb0b264

Browse files
Copilothotlong
andcommitted
Add database field mapping for driver compatibility (better-auth support)
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 86bac43 commit fb0b264

8 files changed

Lines changed: 443 additions & 0 deletions

File tree

content/docs/references/system/AuthConfig.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ description: AuthConfig Schema Reference
2525
| **enterprise** | `object` | optional | |
2626
| **userFieldMapping** | `object` | optional | |
2727
| **database** | `object` | optional | |
28+
| **mapping** | `object` | optional | |
2829
| **plugins** | `object[]` | optional | |
2930
| **hooks** | `object` | optional | Authentication lifecycle hooks |
3031
| **security** | `object` | optional | Advanced security settings |
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
title: DatabaseMapping
3+
description: DatabaseMapping Schema Reference
4+
---
5+
6+
## Properties
7+
8+
| Property | Type | Required | Description |
9+
| :--- | :--- | :--- | :--- |
10+
| **user** | `Record<string, string>` | optional | User field mapping (e.g., `{ "emailVerified": "email_verified" }`) |
11+
| **session** | `Record<string, string>` | optional | Session field mapping |
12+
| **account** | `Record<string, string>` | optional | Account field mapping |
13+
| **verificationToken** | `Record<string, string>` | optional | VerificationToken field mapping |

docs/AUTHENTICATION_STANDARD.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,88 @@ hooks: {
416416
}
417417
```
418418

419+
### Database Field Mapping
420+
421+
The `mapping` configuration allows you to map ObjectStack standard field names (which follow Auth.js conventions) to driver-specific field names. This is particularly useful for ensuring compatibility with authentication libraries like `better-auth` that use different column naming conventions.
422+
423+
```typescript
424+
mapping: {
425+
// User model field mapping (optional)
426+
user?: {
427+
emailVerified: 'email_verified', // Map to snake_case
428+
createdAt: 'created_at',
429+
updatedAt: 'updated_at',
430+
},
431+
432+
// Session model field mapping (default better-auth compatible)
433+
session: {
434+
sessionToken: 'token', // better-auth uses 'token'
435+
expires: 'expiresAt', // better-auth uses 'expiresAt'
436+
},
437+
438+
// Account model field mapping (default better-auth compatible)
439+
account: {
440+
providerAccountId: 'accountId', // better-auth uses 'accountId'
441+
provider: 'providerId', // better-auth uses 'providerId'
442+
},
443+
444+
// Verification token field mapping (optional)
445+
verificationToken?: {
446+
identifier: 'email',
447+
},
448+
}
449+
```
450+
451+
**Default Mappings for better-auth Compatibility:**
452+
453+
By default, the `session` and `account` mappings are pre-configured for `better-auth` compatibility:
454+
455+
| ObjectStack Field | better-auth Field |
456+
|------------------|------------------|
457+
| `sessionToken` | `token` |
458+
| `expires` | `expiresAt` |
459+
| `providerAccountId` | `accountId` |
460+
| `provider` | `providerId` |
461+
462+
You only need to specify the `mapping` configuration if you want to:
463+
- Use a different authentication driver with non-standard field names
464+
- Override the default better-auth mappings
465+
- Add custom mappings for user or verification token fields
466+
467+
**Example with custom mappings:**
468+
469+
```typescript
470+
const authConfig: AuthConfig = {
471+
name: 'custom_auth',
472+
label: 'Custom Auth',
473+
driver: 'custom-driver',
474+
strategies: ['email_password'],
475+
baseUrl: 'https://example.com',
476+
secret: process.env.AUTH_SECRET!,
477+
478+
mapping: {
479+
user: {
480+
emailVerified: 'is_verified',
481+
createdAt: 'created',
482+
updatedAt: 'modified',
483+
},
484+
session: {
485+
sessionToken: 'session_id',
486+
expires: 'expires_at',
487+
},
488+
account: {
489+
providerAccountId: 'external_id',
490+
provider: 'auth_provider',
491+
},
492+
},
493+
494+
session: {},
495+
rateLimit: {},
496+
csrf: {},
497+
accountLinking: {},
498+
};
499+
```
500+
419501
## Supported OAuth Providers
420502

421503
- Google (`google`)

packages/spec/json-schema/AuthConfig.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,48 @@
621621
],
622622
"additionalProperties": false
623623
},
624+
"mapping": {
625+
"type": "object",
626+
"properties": {
627+
"user": {
628+
"type": "object",
629+
"additionalProperties": {
630+
"type": "string"
631+
},
632+
"description": "User field mapping (e.g., { \"emailVerified\": \"email_verified\" })"
633+
},
634+
"session": {
635+
"type": "object",
636+
"additionalProperties": {
637+
"type": "string"
638+
},
639+
"default": {
640+
"sessionToken": "token",
641+
"expires": "expiresAt"
642+
},
643+
"description": "Session field mapping"
644+
},
645+
"account": {
646+
"type": "object",
647+
"additionalProperties": {
648+
"type": "string"
649+
},
650+
"default": {
651+
"providerAccountId": "accountId",
652+
"provider": "providerId"
653+
},
654+
"description": "Account field mapping"
655+
},
656+
"verificationToken": {
657+
"type": "object",
658+
"additionalProperties": {
659+
"type": "string"
660+
},
661+
"description": "VerificationToken field mapping"
662+
}
663+
},
664+
"additionalProperties": false
665+
},
624666
"plugins": {
625667
"type": "array",
626668
"items": {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"$ref": "#/definitions/DatabaseMapping",
3+
"definitions": {
4+
"DatabaseMapping": {
5+
"type": "object",
6+
"properties": {
7+
"user": {
8+
"type": "object",
9+
"additionalProperties": {
10+
"type": "string"
11+
},
12+
"description": "User field mapping (e.g., { \"emailVerified\": \"email_verified\" })"
13+
},
14+
"session": {
15+
"type": "object",
16+
"additionalProperties": {
17+
"type": "string"
18+
},
19+
"default": {
20+
"sessionToken": "token",
21+
"expires": "expiresAt"
22+
},
23+
"description": "Session field mapping"
24+
},
25+
"account": {
26+
"type": "object",
27+
"additionalProperties": {
28+
"type": "string"
29+
},
30+
"default": {
31+
"providerAccountId": "accountId",
32+
"provider": "providerId"
33+
},
34+
"description": "Account field mapping"
35+
},
36+
"verificationToken": {
37+
"type": "object",
38+
"additionalProperties": {
39+
"type": "string"
40+
},
41+
"description": "VerificationToken field mapping"
42+
}
43+
},
44+
"additionalProperties": false
45+
}
46+
},
47+
"$schema": "http://json-schema.org/draft-07/schema#"
48+
}

packages/spec/json-schema/StandardAuthProvider.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,48 @@
629629
],
630630
"additionalProperties": false
631631
},
632+
"mapping": {
633+
"type": "object",
634+
"properties": {
635+
"user": {
636+
"type": "object",
637+
"additionalProperties": {
638+
"type": "string"
639+
},
640+
"description": "User field mapping (e.g., { \"emailVerified\": \"email_verified\" })"
641+
},
642+
"session": {
643+
"type": "object",
644+
"additionalProperties": {
645+
"type": "string"
646+
},
647+
"default": {
648+
"sessionToken": "token",
649+
"expires": "expiresAt"
650+
},
651+
"description": "Session field mapping"
652+
},
653+
"account": {
654+
"type": "object",
655+
"additionalProperties": {
656+
"type": "string"
657+
},
658+
"default": {
659+
"providerAccountId": "accountId",
660+
"provider": "providerId"
661+
},
662+
"description": "Account field mapping"
663+
},
664+
"verificationToken": {
665+
"type": "object",
666+
"additionalProperties": {
667+
"type": "string"
668+
},
669+
"description": "VerificationToken field mapping"
670+
}
671+
},
672+
"additionalProperties": false
673+
},
632674
"plugins": {
633675
"type": "array",
634676
"items": {

0 commit comments

Comments
 (0)