Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/module/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async function loadAuthOptions(context: SchemaContext) {
.filter(([, value]) => typeof value === 'string')
.map(([key, value]) => [key, value as string]),
)
const userConfig = await loadUserAuthConfig(configFile, isProduction, alias)
const userConfig = await loadUserAuthConfig(configFile, isProduction, alias, context.nuxt.options.runtimeConfig)

const extendedConfig: { plugins?: BetterAuthPlugin[] } = {}
await context.nuxt.callHook('better-auth:config:extend', extendedConfig)
Expand Down
3 changes: 2 additions & 1 deletion src/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export async function loadUserAuthConfig(
configPath: string,
throwOnError = false,
alias?: Record<string, string>,
runtimeConfig: unknown = {},
): Promise<Partial<BetterAuthOptions>> {
const { createJiti } = await import('jiti')
const { defineServerAuth: runtimeDefineServerAuth } = await import('./runtime/config')
Expand All @@ -87,7 +88,7 @@ export async function loadUserAuthConfig(
const mod = await jiti.import(configPath) as { default?: unknown }
const configFn = mod.default
if (typeof configFn === 'function') {
return configFn({ runtimeConfig: {}, db: null })
return configFn({ runtimeConfig, db: null })
}
consola.warn('[@onmax/nuxt-better-auth] auth.config.ts does not export default. Expected: export default defineServerAuth(...)')
if (throwOnError) {
Expand Down
5 changes: 4 additions & 1 deletion test/cases/auth-schema-export/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export default defineNuxtConfig({

runtimeConfig: {
betterAuthSecret: 'test-secret-for-testing-only-32chars!',
public: { siteUrl: 'http://localhost:3000' },
public: {
app: { routes: { signUp: '/auth/sign-up' } },
siteUrl: 'http://localhost:3000',
},
},
})
6 changes: 3 additions & 3 deletions test/cases/auth-schema-export/server/auth.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineServerAuth } from '../../../../src/runtime/config'

export default defineServerAuth({
appName: 'Auth Schema Export Test',
export default defineServerAuth(({ runtimeConfig }) => ({
appName: runtimeConfig.public.app.routes.signUp,
emailAndPassword: { enabled: true },
})
}))
11 changes: 11 additions & 0 deletions test/schema-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ describe('loadUserAuthConfig', () => {

expect(result).toEqual({ plugins: [] })
})

it('passes runtime config to function syntax during schema generation', async () => {
const configPath = join(TEST_DIR, 'runtime-config.ts')
writeFileSync(configPath, `export default defineServerAuth(({ runtimeConfig }) => ({ appName: runtimeConfig.public.app.routes.signUp }))`)

const result = await loadUserAuthConfig(configPath, false, undefined, {
public: { app: { routes: { signUp: '/auth/sign-up' } } },
})

expect(result).toEqual({ appName: '/auth/sign-up' })
})
})

describe('defineServerAuth', () => {
Expand Down
Loading