Skip to content

Commit 642e58c

Browse files
committed
fix: use virtual module for hub:kv to avoid build errors
1 parent 9b043c2 commit 642e58c

2 files changed

Lines changed: 31 additions & 19 deletions

File tree

src/module.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,35 @@ export default defineNuxtModule<BetterAuthModuleOptions>({
6262
nuxt.options.alias['#auth/server'] = serverConfigPath
6363
nuxt.options.alias['#auth/client'] = clientConfigPath
6464

65+
// Generate secondary storage virtual module (conditional hub:kv)
66+
const secondaryStorageCode = secondaryStorageEnabled
67+
? `import { kv } from 'hub:kv'
68+
export function createSecondaryStorage() {
69+
return {
70+
get: async (key) => kv.get(\`_auth:\${key}\`),
71+
set: async (key, value, ttl) => kv.set(\`_auth:\${key}\`, value, { ttl }),
72+
delete: async (key) => kv.del(\`_auth:\${key}\`),
73+
}
74+
}`
75+
: `export function createSecondaryStorage() { return undefined }`
76+
77+
const secondaryStorageTemplate = addTemplate({ filename: 'better-auth/secondary-storage.mjs', getContents: () => secondaryStorageCode, write: true })
78+
nuxt.options.alias['#auth/secondary-storage'] = secondaryStorageTemplate.dst
79+
80+
addTypeTemplate({
81+
filename: 'types/auth-secondary-storage.d.ts',
82+
getContents: () => `
83+
declare module '#auth/secondary-storage' {
84+
interface SecondaryStorage {
85+
get: (key: string) => Promise<string | null>
86+
set: (key: string, value: unknown, ttl?: number) => Promise<void>
87+
delete: (key: string) => Promise<void>
88+
}
89+
export function createSecondaryStorage(): SecondaryStorage | undefined
90+
}
91+
`,
92+
})
93+
6594
// Add type template for #nuxt-better-auth module augmentation
6695
addTypeTemplate({
6796
filename: 'types/nuxt-better-auth.d.ts',

src/runtime/server/utils/auth.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createSecondaryStorage } from '#auth/secondary-storage'
12
import createServerAuth from '#auth/server'
23
import { useEvent, useRuntimeConfig } from '#imports'
34
import { betterAuth } from 'better-auth'
@@ -21,24 +22,6 @@ function getBaseURL(siteUrl?: string): string {
2122
}
2223
}
2324

24-
let _kv: typeof import('hub:kv').kv | undefined
25-
26-
async function getKV() {
27-
if (!_kv) {
28-
const { kv } = await import('hub:kv')
29-
_kv = kv
30-
}
31-
return _kv
32-
}
33-
34-
function createSecondaryStorage() {
35-
return {
36-
get: async (key: string) => (await getKV()).get(`_auth:${key}`),
37-
set: async (key: string, value: unknown, ttl?: number) => (await getKV()).set(`_auth:${key}`, value, { ttl }),
38-
delete: async (key: string) => (await getKV()).del(`_auth:${key}`),
39-
}
40-
}
41-
4225
export async function serverAuth(): Promise<AuthInstance> {
4326
if (_auth)
4427
return _auth
@@ -64,7 +47,7 @@ export async function serverAuth(): Promise<AuthInstance> {
6447
_auth = betterAuth({
6548
...userConfig,
6649
...(database && { database }),
67-
secondaryStorage: authConfig?.secondaryStorage ? createSecondaryStorage() : undefined,
50+
secondaryStorage: createSecondaryStorage(),
6851
secret: runtimeConfig.betterAuthSecret,
6952
baseURL: getBaseURL(runtimeConfig.public.siteUrl as string | undefined),
7053
})

0 commit comments

Comments
 (0)