Skip to content

Commit 0fbb648

Browse files
authored
fix: load env before auth schema config (#379)
1 parent e0e813a commit 0fbb648

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

src/module/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async function loadAuthOptions(context: SchemaContext) {
7373
.filter(([, value]) => typeof value === 'string')
7474
.map(([key, value]) => [key, value as string]),
7575
)
76-
const userConfig = await loadUserAuthConfig(configFile, isProduction, alias, context.nuxt.options.runtimeConfig)
76+
const userConfig = await loadUserAuthConfig(configFile, isProduction, alias, context.nuxt.options.runtimeConfig, context.nuxt.options.rootDir)
7777

7878
const extendedConfig: { plugins?: BetterAuthPlugin[] } = {}
7979
await context.nuxt.callHook('better-auth:config:extend', extendedConfig)

src/schema-generator.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { BetterAuthOptions } from 'better-auth'
22
import type { Casing } from 'drizzle-orm/utils'
3+
import { existsSync } from 'node:fs'
34
import { generateDrizzleSchema as _generateDrizzleSchema } from '@better-auth/cli/api'
45
import { consola } from 'consola'
6+
import { join } from 'pathe'
57

68
export interface SchemaOptions { usePlural?: boolean, useUuid?: boolean, casing?: Casing }
79

@@ -59,6 +61,17 @@ interface SchemaGeneratorGlobals {
5961
defineServerAuth?: RuntimeDefineServerAuthFn
6062
}
6163

64+
function loadLocalEnv(rootDir?: string): void {
65+
if (!rootDir)
66+
return
67+
68+
const envPath = join(rootDir, '.env.local')
69+
if (!existsSync(envPath))
70+
return
71+
72+
process.loadEnvFile(envPath)
73+
}
74+
6275
declare global {
6376
// eslint-disable-next-line vars-on-top
6477
var __nuxtBetterAuthDefineServerAuth: RuntimeDefineServerAuthFn | undefined
@@ -69,6 +82,7 @@ export async function loadUserAuthConfig(
6982
throwOnError = false,
7083
alias?: Record<string, string>,
7184
runtimeConfig: unknown = {},
85+
rootDir?: string,
7286
): Promise<Partial<BetterAuthOptions>> {
7387
const { createJiti } = await import('jiti')
7488
const { defineServerAuth: runtimeDefineServerAuth } = await import('./runtime/config')
@@ -85,6 +99,7 @@ export async function loadUserAuthConfig(
8599
schemaGlobals.__nuxtBetterAuthDefineServerAuth!._count++
86100

87101
try {
102+
loadLocalEnv(rootDir)
88103
const mod = await jiti.import(configPath) as { default?: unknown }
89104
const configFn = mod.default
90105
if (typeof configFn === 'function') {

test/schema-generator.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,30 @@ describe('loadUserAuthConfig', () => {
160160

161161
expect(result).toEqual({ appName: '/auth/sign-up' })
162162
})
163+
164+
it('loads .env.local before importing auth config dependencies', async () => {
165+
const envPath = join(TEST_DIR, '.env.local')
166+
const helperPath = join(TEST_DIR, 'env-helper.ts')
167+
const configPath = join(TEST_DIR, 'env-config.ts')
168+
const originalApiKey = process.env.RESEND_API_KEY
169+
delete process.env.RESEND_API_KEY
170+
171+
try {
172+
writeFileSync(envPath, 'RESEND_API_KEY=re_test_fixture\n')
173+
writeFileSync(helperPath, `if (!process.env.RESEND_API_KEY)\n throw new Error('Missing API key. Pass it to the constructor new Resend("re_123")')\n\nexport function getPlugins() { return [] }\n`)
174+
writeFileSync(configPath, `import { getPlugins } from './env-helper'\nexport default defineServerAuth({ plugins: getPlugins() })`)
175+
176+
const result = await loadUserAuthConfig(configPath, true, undefined, {}, TEST_DIR)
177+
178+
expect(result).toEqual({ plugins: [] })
179+
}
180+
finally {
181+
if (originalApiKey === undefined)
182+
delete process.env.RESEND_API_KEY
183+
else
184+
process.env.RESEND_API_KEY = originalApiKey
185+
}
186+
})
163187
})
164188

165189
describe('defineServerAuth', () => {

0 commit comments

Comments
 (0)