@@ -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' ,
0 commit comments