This repository was archived by the owner on Mar 1, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
fix(better-auth): support custom table enum field types and defaults #621
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { zenstackAdapter } from '../src/adapter'; | ||
| import { betterAuth } from 'better-auth'; | ||
|
|
||
| export const auth = betterAuth({ | ||
| database: zenstackAdapter({} as any, { | ||
| provider: 'postgresql', | ||
| }), | ||
| user: { | ||
| additionalFields: { | ||
| role: { | ||
| type: ['user', 'admin'], | ||
| required: false, | ||
| defaultValue: 'user', | ||
| input: false, // don't allow user to set role | ||
| }, | ||
| lang: { | ||
| type: 'string', | ||
| required: false, | ||
| defaultValue: 'en', | ||
| }, | ||
| age: { | ||
| type: 'number', | ||
| required: true, | ||
| defaultValue: 18, | ||
| }, | ||
| admin: { | ||
| type: 'boolean', | ||
| required: false, | ||
| defaultValue: false, | ||
| }, | ||
| }, | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { zenstackAdapter } from '../src/adapter'; | ||
| import { betterAuth } from 'better-auth'; | ||
|
|
||
| export const auth = betterAuth({ | ||
| database: zenstackAdapter({} as any, { | ||
| provider: 'postgresql', | ||
| }), | ||
| }); | ||
65 changes: 65 additions & 0 deletions
65
packages/auth-adapters/better-auth/test/cli-generate.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { execSync } from 'node:child_process'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import tmp from 'tmp'; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = path.dirname(__filename); | ||
|
|
||
| /** | ||
| * Helper function to generate schema using better-auth CLI | ||
| */ | ||
| function generateSchema(configFile: string): string { | ||
| const { name: workDir } = tmp.dirSync({ unsafeCleanup: true }); | ||
| const schemaPath = path.join(workDir, 'schema.zmodel'); | ||
| const configPath = path.join(__dirname, configFile); | ||
|
|
||
| execSync(`pnpm better-auth generate --config ${configPath} --output ${schemaPath} --yes`, { | ||
|
|
||
| cwd: __dirname, | ||
| stdio: 'pipe', | ||
| }); | ||
|
ymc9 marked this conversation as resolved.
|
||
|
|
||
| return schemaPath; | ||
|
ymc9 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** | ||
| * Helper function to verify schema with zenstack check | ||
| */ | ||
| function verifySchema(schemaPath: string) { | ||
| const cliPath = path.join(__dirname, '../../../cli/dist/index.js'); | ||
| const workDir = path.dirname(schemaPath); | ||
|
|
||
| expect(fs.existsSync(schemaPath)).toBe(true); | ||
|
|
||
| expect(() => { | ||
| execSync(`node ${cliPath} check --schema ${schemaPath}`, { | ||
|
|
||
| cwd: workDir, | ||
| stdio: 'pipe', | ||
| }); | ||
| }).not.toThrow(); | ||
| } | ||
|
|
||
| describe('Cli schema generation tests', () => { | ||
| it('works with simple config', async () => { | ||
| const schemaPath = generateSchema('auth.ts'); | ||
| verifySchema(schemaPath); | ||
| }); | ||
|
|
||
| it('works with custom config', async () => { | ||
| const schemaPath = generateSchema('auth-custom.ts'); | ||
| verifySchema(schemaPath); | ||
|
|
||
| // Verify that the generated schema contains the expected default values | ||
| const schemaContent = fs.readFileSync(schemaPath, 'utf-8'); | ||
| expect(schemaContent).toMatch(/role\s+String/); | ||
| expect(schemaContent).toContain("@default('user')"); | ||
| expect(schemaContent).toMatch(/lang\s+String/); | ||
| expect(schemaContent).toContain("@default('en')"); | ||
| expect(schemaContent).toMatch(/age\s+Int/); | ||
| expect(schemaContent).toContain('@default(18)'); | ||
| expect(schemaContent).toMatch(/admin\s+Boolean/); | ||
| expect(schemaContent).toContain('@default(false)'); | ||
| }); | ||
|
ymc9 marked this conversation as resolved.
|
||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.