@onmax/nuxt-better-auth 0.0.2-alpha.10 → 0.0.2-alpha.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/module.json +1 -1
- package/dist/module.mjs +124 -58
- package/dist/runtime/app/components/BetterAuthState.vue +1 -0
- package/dist/runtime/app/plugins/session.client.js +1 -2
- package/dist/runtime/config.d.ts +4 -0
- package/dist/runtime/server/api/_better-auth/config.get.js +2 -2
- package/dist/runtime/server/api/auth/[...all].js +1 -1
- package/dist/runtime/server/utils/auth.d.ts +1 -7
- package/dist/runtime/server/utils/auth.js +10 -18
- package/dist/runtime/server/utils/session.js +1 -1
- package/dist/runtime/types.d.ts +1 -1
- package/package.json +1 -1
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { existsSync } from 'node:fs';
|
|
2
2
|
import { mkdir, writeFile } from 'node:fs/promises';
|
|
3
|
-
import { defineNuxtModule, createResolver, hasNuxtModule, addTemplate, addTypeTemplate, updateTemplates, addServerImportsDir, addServerScanDir, addServerHandler, addImportsDir, addPlugin, addComponentsDir, extendPages } from '@nuxt/kit';
|
|
3
|
+
import { defineNuxtModule, createResolver, hasNuxtModule, addTemplate, addTypeTemplate, updateTemplates, addServerImportsDir, addServerImports, addServerScanDir, addServerHandler, addImportsDir, addPlugin, addComponentsDir, extendPages } from '@nuxt/kit';
|
|
4
4
|
import { consola as consola$1 } from 'consola';
|
|
5
5
|
import { defu } from 'defu';
|
|
6
6
|
import { join } from 'pathe';
|
|
@@ -162,7 +162,15 @@ function getMysqlType(type, fieldName) {
|
|
|
162
162
|
}
|
|
163
163
|
async function loadUserAuthConfig(configPath, throwOnError = false) {
|
|
164
164
|
const { createJiti } = await import('jiti');
|
|
165
|
-
const
|
|
165
|
+
const { defineServerAuth } = await import('../dist/runtime/config.js');
|
|
166
|
+
const jiti = createJiti(import.meta.url, { interopDefault: true, moduleCache: false });
|
|
167
|
+
const key = "defineServerAuth";
|
|
168
|
+
const g = globalThis;
|
|
169
|
+
if (!g[key]) {
|
|
170
|
+
defineServerAuth._count = 0;
|
|
171
|
+
g[key] = defineServerAuth;
|
|
172
|
+
}
|
|
173
|
+
g[key]._count++;
|
|
166
174
|
try {
|
|
167
175
|
const mod = await jiti.import(configPath);
|
|
168
176
|
const configFn = typeof mod === "object" && mod !== null && "default" in mod ? mod.default : mod;
|
|
@@ -179,6 +187,11 @@ async function loadUserAuthConfig(configPath, throwOnError = false) {
|
|
|
179
187
|
}
|
|
180
188
|
consola$1.error("[@onmax/nuxt-better-auth] Failed to load auth config for schema generation. Schema may be incomplete:", error);
|
|
181
189
|
return {};
|
|
190
|
+
} finally {
|
|
191
|
+
g[key]._count--;
|
|
192
|
+
if (!g[key]._count) {
|
|
193
|
+
delete g[key];
|
|
194
|
+
}
|
|
182
195
|
}
|
|
183
196
|
}
|
|
184
197
|
|
|
@@ -200,6 +213,7 @@ const consola = consola$1.withTag("nuxt-better-auth");
|
|
|
200
213
|
const module$1 = defineNuxtModule({
|
|
201
214
|
meta: { name: "@onmax/nuxt-better-auth", configKey: "auth", compatibility: { nuxt: ">=3.0.0" } },
|
|
202
215
|
defaults: {
|
|
216
|
+
clientOnly: false,
|
|
203
217
|
serverConfig: "server/auth.config",
|
|
204
218
|
clientConfig: "app/auth.config",
|
|
205
219
|
redirects: { login: "/login", guest: "/" },
|
|
@@ -207,44 +221,63 @@ const module$1 = defineNuxtModule({
|
|
|
207
221
|
},
|
|
208
222
|
async setup(options, nuxt) {
|
|
209
223
|
const resolver = createResolver(import.meta.url);
|
|
224
|
+
const clientOnly = options.clientOnly;
|
|
210
225
|
const serverConfigFile = options.serverConfig;
|
|
211
226
|
const clientConfigFile = options.clientConfig;
|
|
212
227
|
const serverConfigPath = resolver.resolve(nuxt.options.rootDir, serverConfigFile);
|
|
213
228
|
const clientConfigPath = resolver.resolve(nuxt.options.rootDir, clientConfigFile);
|
|
214
229
|
const serverConfigExists = existsSync(`${serverConfigPath}.ts`) || existsSync(`${serverConfigPath}.js`);
|
|
215
230
|
const clientConfigExists = existsSync(`${clientConfigPath}.ts`) || existsSync(`${clientConfigPath}.js`);
|
|
216
|
-
if (!serverConfigExists)
|
|
231
|
+
if (!clientOnly && !serverConfigExists)
|
|
217
232
|
throw new Error(`[nuxt-better-auth] Missing ${serverConfigFile}.ts - create with defineServerAuth()`);
|
|
218
233
|
if (!clientConfigExists)
|
|
219
234
|
throw new Error(`[nuxt-better-auth] Missing ${clientConfigFile}.ts - export createAppAuthClient()`);
|
|
220
235
|
const hasNuxtHub = hasNuxtModule("@nuxthub/core", nuxt);
|
|
221
236
|
const hub = hasNuxtHub ? nuxt.options.hub : void 0;
|
|
222
|
-
const hasHubDb = hasNuxtHub && !!hub?.db;
|
|
237
|
+
const hasHubDb = !clientOnly && hasNuxtHub && !!hub?.db;
|
|
223
238
|
let secondaryStorageEnabled = options.secondaryStorage ?? false;
|
|
224
|
-
if (secondaryStorageEnabled &&
|
|
239
|
+
if (secondaryStorageEnabled && clientOnly) {
|
|
240
|
+
consola.warn("secondaryStorage is not available in clientOnly mode. Disabling.");
|
|
241
|
+
secondaryStorageEnabled = false;
|
|
242
|
+
} else if (secondaryStorageEnabled && (!hasNuxtHub || !hub?.kv)) {
|
|
225
243
|
consola.warn("secondaryStorage requires @nuxthub/core with hub.kv: true. Disabling.");
|
|
226
244
|
secondaryStorageEnabled = false;
|
|
227
245
|
}
|
|
228
246
|
nuxt.options.runtimeConfig.public = nuxt.options.runtimeConfig.public || {};
|
|
229
247
|
nuxt.options.runtimeConfig.public.auth = defu(nuxt.options.runtimeConfig.public.auth, {
|
|
230
248
|
redirects: { login: options.redirects?.login ?? "/login", guest: options.redirects?.guest ?? "/" },
|
|
231
|
-
useDatabase: hasHubDb
|
|
249
|
+
useDatabase: hasHubDb,
|
|
250
|
+
clientOnly
|
|
232
251
|
});
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
252
|
+
if (clientOnly) {
|
|
253
|
+
const siteUrl = process.env.NUXT_PUBLIC_SITE_URL || nuxt.options.runtimeConfig.public.siteUrl;
|
|
254
|
+
if (!siteUrl) {
|
|
255
|
+
consola.warn("clientOnly mode: NUXT_PUBLIC_SITE_URL should be set to your frontend URL");
|
|
256
|
+
}
|
|
257
|
+
consola.info("clientOnly mode enabled - server utilities (serverAuth, getUserSession, requireUserSession) are not available");
|
|
236
258
|
}
|
|
237
|
-
if (
|
|
238
|
-
|
|
259
|
+
if (!clientOnly) {
|
|
260
|
+
const betterAuthSecret = process.env.BETTER_AUTH_SECRET || process.env.NUXT_BETTER_AUTH_SECRET || nuxt.options.runtimeConfig.betterAuthSecret || "";
|
|
261
|
+
if (!nuxt.options.dev && !betterAuthSecret) {
|
|
262
|
+
throw new Error("[nuxt-better-auth] BETTER_AUTH_SECRET is required in production. Set BETTER_AUTH_SECRET or NUXT_BETTER_AUTH_SECRET environment variable.");
|
|
263
|
+
}
|
|
264
|
+
if (betterAuthSecret && betterAuthSecret.length < 32) {
|
|
265
|
+
throw new Error("[nuxt-better-auth] BETTER_AUTH_SECRET must be at least 32 characters for security");
|
|
266
|
+
}
|
|
267
|
+
nuxt.options.runtimeConfig.betterAuthSecret = betterAuthSecret;
|
|
268
|
+
nuxt.options.runtimeConfig.auth = defu(nuxt.options.runtimeConfig.auth, {
|
|
269
|
+
secondaryStorage: secondaryStorageEnabled
|
|
270
|
+
});
|
|
239
271
|
}
|
|
240
|
-
nuxt.options.runtimeConfig.betterAuthSecret = betterAuthSecret;
|
|
241
|
-
nuxt.options.runtimeConfig.auth = defu(nuxt.options.runtimeConfig.auth, {
|
|
242
|
-
secondaryStorage: secondaryStorageEnabled
|
|
243
|
-
});
|
|
244
272
|
nuxt.options.alias["#nuxt-better-auth"] = resolver.resolve("./runtime/types/augment");
|
|
245
|
-
|
|
273
|
+
if (!clientOnly)
|
|
274
|
+
nuxt.options.alias["#auth/server"] = serverConfigPath;
|
|
246
275
|
nuxt.options.alias["#auth/client"] = clientConfigPath;
|
|
247
|
-
|
|
276
|
+
if (!clientOnly) {
|
|
277
|
+
if (secondaryStorageEnabled && !nuxt.options.alias["hub:kv"]) {
|
|
278
|
+
throw new Error("[nuxt-better-auth] hub:kv not found. Ensure @nuxthub/core is loaded before this module and hub.kv is enabled.");
|
|
279
|
+
}
|
|
280
|
+
const secondaryStorageCode = secondaryStorageEnabled ? `import { kv } from '../hub/kv.mjs'
|
|
248
281
|
export function createSecondaryStorage() {
|
|
249
282
|
return {
|
|
250
283
|
get: async (key) => kv.get(\`_auth:\${key}\`),
|
|
@@ -252,21 +285,24 @@ export function createSecondaryStorage() {
|
|
|
252
285
|
delete: async (key) => kv.del(\`_auth:\${key}\`),
|
|
253
286
|
}
|
|
254
287
|
}` : `export function createSecondaryStorage() { return undefined }`;
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
288
|
+
const secondaryStorageTemplate = addTemplate({ filename: "better-auth/secondary-storage.mjs", getContents: () => secondaryStorageCode, write: true });
|
|
289
|
+
nuxt.options.alias["#auth/secondary-storage"] = secondaryStorageTemplate.dst;
|
|
290
|
+
if (hasHubDb && !nuxt.options.alias["hub:db"]) {
|
|
291
|
+
throw new Error("[nuxt-better-auth] hub:db not found. Ensure @nuxthub/core is loaded before this module and hub.db is configured.");
|
|
292
|
+
}
|
|
293
|
+
const hubDialect = getHubDialect(hub) ?? "sqlite";
|
|
294
|
+
const databaseCode = hasHubDb ? `import { db, schema } from '../hub/db.mjs'
|
|
259
295
|
import { drizzleAdapter } from 'better-auth/adapters/drizzle'
|
|
260
296
|
const rawDialect = '${hubDialect}'
|
|
261
297
|
const dialect = rawDialect === 'postgresql' ? 'pg' : rawDialect
|
|
262
298
|
export function createDatabase() { return drizzleAdapter(db, { provider: dialect, schema }) }
|
|
263
299
|
export { db }` : `export function createDatabase() { return undefined }
|
|
264
300
|
export const db = undefined`;
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
301
|
+
const databaseTemplate = addTemplate({ filename: "better-auth/database.mjs", getContents: () => databaseCode, write: true });
|
|
302
|
+
nuxt.options.alias["#auth/database"] = databaseTemplate.dst;
|
|
303
|
+
addTypeTemplate({
|
|
304
|
+
filename: "types/auth-secondary-storage.d.ts",
|
|
305
|
+
getContents: () => `
|
|
270
306
|
declare module '#auth/secondary-storage' {
|
|
271
307
|
interface SecondaryStorage {
|
|
272
308
|
get: (key: string) => Promise<string | null>
|
|
@@ -276,28 +312,21 @@ declare module '#auth/secondary-storage' {
|
|
|
276
312
|
export function createSecondaryStorage(): SecondaryStorage | undefined
|
|
277
313
|
}
|
|
278
314
|
`
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
315
|
+
});
|
|
316
|
+
addTypeTemplate({
|
|
317
|
+
filename: "types/auth-database.d.ts",
|
|
318
|
+
getContents: () => `
|
|
283
319
|
declare module '#auth/database' {
|
|
284
320
|
import type { drizzleAdapter } from 'better-auth/adapters/drizzle'
|
|
285
321
|
export function createDatabase(): ReturnType<typeof drizzleAdapter> | undefined
|
|
286
322
|
export const db: unknown
|
|
287
323
|
}
|
|
288
324
|
`
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
export type { AuthMeta, AuthMode, AuthRouteRules, UserMatch, RequireSessionOptions, Auth, InferUser, InferSession } from '${resolver.resolve("./runtime/types")}'
|
|
295
|
-
`
|
|
296
|
-
});
|
|
297
|
-
addTypeTemplate({
|
|
298
|
-
filename: "types/nuxt-better-auth-infer.d.ts",
|
|
299
|
-
getContents: () => `
|
|
300
|
-
import type { InferUser, InferSession } from 'better-auth'
|
|
325
|
+
});
|
|
326
|
+
addTypeTemplate({
|
|
327
|
+
filename: "types/nuxt-better-auth-infer.d.ts",
|
|
328
|
+
getContents: () => `
|
|
329
|
+
import type { InferUser, InferSession, InferPluginTypes } from 'better-auth'
|
|
301
330
|
import type { RuntimeConfig } from 'nuxt/schema'
|
|
302
331
|
import type configFn from '${serverConfigPath}'
|
|
303
332
|
|
|
@@ -305,12 +334,55 @@ type _Config = ReturnType<typeof configFn>
|
|
|
305
334
|
|
|
306
335
|
declare module '#nuxt-better-auth' {
|
|
307
336
|
interface AuthUser extends InferUser<_Config> {}
|
|
308
|
-
interface AuthSession
|
|
337
|
+
interface AuthSession extends InferSession<_Config> {}
|
|
309
338
|
interface ServerAuthContext {
|
|
310
339
|
runtimeConfig: RuntimeConfig
|
|
311
340
|
${hasHubDb ? `db: typeof import('hub:db')['db']` : ""}
|
|
312
341
|
}
|
|
342
|
+
type PluginTypes = InferPluginTypes<_Config>
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// Augment the config module to use the extended ServerAuthContext
|
|
346
|
+
interface _AugmentedServerAuthContext {
|
|
347
|
+
runtimeConfig: RuntimeConfig
|
|
348
|
+
${hasHubDb ? `db: typeof import('hub:db')['db']` : "db: unknown"}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
declare module '@onmax/nuxt-better-auth/config' {
|
|
352
|
+
import type { BetterAuthOptions } from 'better-auth'
|
|
353
|
+
type ServerAuthConfig = Omit<BetterAuthOptions, 'database' | 'secret' | 'baseURL'>
|
|
354
|
+
export function defineServerAuth<T extends ServerAuthConfig>(config: (ctx: _AugmentedServerAuthContext) => T): (ctx: _AugmentedServerAuthContext) => T
|
|
313
355
|
}
|
|
356
|
+
`
|
|
357
|
+
});
|
|
358
|
+
addTypeTemplate({
|
|
359
|
+
filename: "types/nuxt-better-auth-nitro.d.ts",
|
|
360
|
+
getContents: () => `
|
|
361
|
+
declare module 'nitropack' {
|
|
362
|
+
interface NitroRouteRules {
|
|
363
|
+
auth?: import('${resolver.resolve("./runtime/types")}').AuthMeta
|
|
364
|
+
}
|
|
365
|
+
interface NitroRouteConfig {
|
|
366
|
+
auth?: import('${resolver.resolve("./runtime/types")}').AuthMeta
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
declare module 'nitropack/types' {
|
|
370
|
+
interface NitroRouteRules {
|
|
371
|
+
auth?: import('${resolver.resolve("./runtime/types")}').AuthMeta
|
|
372
|
+
}
|
|
373
|
+
interface NitroRouteConfig {
|
|
374
|
+
auth?: import('${resolver.resolve("./runtime/types")}').AuthMeta
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
export {}
|
|
378
|
+
`
|
|
379
|
+
}, { nuxt: true, nitro: true, node: true });
|
|
380
|
+
}
|
|
381
|
+
addTypeTemplate({
|
|
382
|
+
filename: "types/nuxt-better-auth.d.ts",
|
|
383
|
+
getContents: () => `
|
|
384
|
+
export * from '${resolver.resolve("./runtime/types/augment")}'
|
|
385
|
+
export type { AuthMeta, AuthMode, AuthRouteRules, UserMatch, RequireSessionOptions, Auth, InferUser, InferSession } from '${resolver.resolve("./runtime/types")}'
|
|
314
386
|
`
|
|
315
387
|
});
|
|
316
388
|
addTypeTemplate({
|
|
@@ -320,16 +392,6 @@ import type { createAppAuthClient } from '${clientConfigPath}'
|
|
|
320
392
|
declare module '#nuxt-better-auth' {
|
|
321
393
|
export type AppAuthClient = ReturnType<typeof createAppAuthClient>
|
|
322
394
|
}
|
|
323
|
-
`
|
|
324
|
-
});
|
|
325
|
-
addTypeTemplate({
|
|
326
|
-
filename: "types/nuxt-better-auth-nitro.d.ts",
|
|
327
|
-
getContents: () => `
|
|
328
|
-
declare module 'nitropack/types' {
|
|
329
|
-
interface NitroRouteRules {
|
|
330
|
-
auth?: import('${resolver.resolve("./runtime/types")}').AuthMeta
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
395
|
`
|
|
334
396
|
});
|
|
335
397
|
nuxt.hook("builder:watch", async (_event, relativePath) => {
|
|
@@ -337,12 +399,16 @@ declare module 'nitropack/types' {
|
|
|
337
399
|
await updateTemplates({ filter: (t) => t.filename.includes("nuxt-better-auth") });
|
|
338
400
|
}
|
|
339
401
|
});
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
402
|
+
if (!clientOnly) {
|
|
403
|
+
addServerImportsDir(resolver.resolve("./runtime/server/utils"));
|
|
404
|
+
addServerImports([{ name: "defineServerAuth", from: resolver.resolve("./runtime/config") }]);
|
|
405
|
+
addServerScanDir(resolver.resolve("./runtime/server/middleware"));
|
|
406
|
+
addServerHandler({ route: "/api/auth/**", handler: resolver.resolve("./runtime/server/api/auth/[...all]") });
|
|
407
|
+
}
|
|
343
408
|
addImportsDir(resolver.resolve("./runtime/app/composables"));
|
|
344
409
|
addImportsDir(resolver.resolve("./runtime/utils"));
|
|
345
|
-
|
|
410
|
+
if (!clientOnly)
|
|
411
|
+
addPlugin({ src: resolver.resolve("./runtime/app/plugins/session.server"), mode: "server" });
|
|
346
412
|
addPlugin({ src: resolver.resolve("./runtime/app/plugins/session.client"), mode: "client" });
|
|
347
413
|
addComponentsDir({ path: resolver.resolve("./runtime/app/components") });
|
|
348
414
|
nuxt.hook("app:resolve", (app) => {
|
|
@@ -352,7 +418,7 @@ declare module 'nitropack/types' {
|
|
|
352
418
|
await setupBetterAuthSchema(nuxt, serverConfigPath, options);
|
|
353
419
|
}
|
|
354
420
|
const isProduction = process.env.NODE_ENV === "production" || !nuxt.options.dev;
|
|
355
|
-
if (!isProduction) {
|
|
421
|
+
if (!isProduction && !clientOnly) {
|
|
356
422
|
setupDevTools(nuxt);
|
|
357
423
|
addServerHandler({ route: "/api/_better-auth/config", method: "get", handler: resolver.resolve("./runtime/server/api/_better-auth/config.get") });
|
|
358
424
|
if (hasHubDb) {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { defineNuxtPlugin } from "#imports";
|
|
2
|
-
import { useUserSession } from "../composables/useUserSession.js";
|
|
1
|
+
import { defineNuxtPlugin, useUserSession } from "#imports";
|
|
3
2
|
export default defineNuxtPlugin(async (nuxtApp) => {
|
|
4
3
|
const { fetchSession } = useUserSession();
|
|
5
4
|
const safeFetch = async () => {
|
package/dist/runtime/config.d.ts
CHANGED
|
@@ -13,6 +13,8 @@ type ClientAuthConfig = Omit<ClientOptions, 'baseURL'> & {
|
|
|
13
13
|
export type ServerAuthConfigFn = (ctx: ServerAuthContext) => ServerAuthConfig;
|
|
14
14
|
export type ClientAuthConfigFn = (ctx: ClientAuthContext) => ClientAuthConfig;
|
|
15
15
|
export interface BetterAuthModuleOptions {
|
|
16
|
+
/** Client-only mode - skip server setup for external auth backends */
|
|
17
|
+
clientOnly?: boolean;
|
|
16
18
|
/** Server config path relative to rootDir. Default: 'server/auth.config' */
|
|
17
19
|
serverConfig?: string;
|
|
18
20
|
/** Client config path relative to rootDir. Default: 'app/auth.config' */
|
|
@@ -36,6 +38,8 @@ export interface AuthRuntimeConfig {
|
|
|
36
38
|
login: string;
|
|
37
39
|
guest: string;
|
|
38
40
|
};
|
|
41
|
+
useDatabase: boolean;
|
|
42
|
+
clientOnly: boolean;
|
|
39
43
|
}
|
|
40
44
|
export interface AuthPrivateRuntimeConfig {
|
|
41
45
|
secondaryStorage: boolean;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { defineEventHandler } from "h3";
|
|
2
2
|
import { useRuntimeConfig } from "nitropack/runtime";
|
|
3
3
|
import { serverAuth } from "../../utils/auth.js";
|
|
4
|
-
export default defineEventHandler(async (
|
|
4
|
+
export default defineEventHandler(async () => {
|
|
5
5
|
try {
|
|
6
|
-
const auth =
|
|
6
|
+
const auth = serverAuth();
|
|
7
7
|
const options = auth.options;
|
|
8
8
|
const runtimeConfig = useRuntimeConfig();
|
|
9
9
|
const publicAuth = runtimeConfig.public?.auth;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { defineEventHandler, toWebRequest } from "h3";
|
|
2
2
|
import { serverAuth } from "../../utils/auth.js";
|
|
3
3
|
export default defineEventHandler(async (event) => {
|
|
4
|
-
const auth =
|
|
4
|
+
const auth = serverAuth();
|
|
5
5
|
return auth.handler(toWebRequest(event));
|
|
6
6
|
});
|
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
import type { Auth } from 'better-auth';
|
|
2
|
-
import type { H3Event } from 'h3';
|
|
3
2
|
import createServerAuth from '#auth/server';
|
|
4
3
|
type AuthInstance = Auth<ReturnType<typeof createServerAuth>>;
|
|
5
|
-
declare
|
|
6
|
-
interface H3EventContext {
|
|
7
|
-
_betterAuth?: AuthInstance;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
export declare function serverAuth(event: H3Event): Promise<AuthInstance>;
|
|
4
|
+
export declare function serverAuth(): AuthInstance;
|
|
11
5
|
export {};
|
|
@@ -2,31 +2,23 @@ import { createDatabase, db } from "#auth/database";
|
|
|
2
2
|
import { createSecondaryStorage } from "#auth/secondary-storage";
|
|
3
3
|
import createServerAuth from "#auth/server";
|
|
4
4
|
import { betterAuth } from "better-auth";
|
|
5
|
-
import { consola } from "consola";
|
|
6
|
-
import { getRequestURL } from "h3";
|
|
7
5
|
import { useRuntimeConfig } from "nitropack/runtime";
|
|
8
|
-
|
|
9
|
-
function
|
|
10
|
-
if (
|
|
11
|
-
return
|
|
12
|
-
const origin = getRequestURL(event).origin;
|
|
13
|
-
if (process.env.NODE_ENV === "production")
|
|
14
|
-
throw new Error("siteUrl must be configured in production. Set NUXT_PUBLIC_SITE_URL or configure in nuxt.config.");
|
|
15
|
-
logger.warn("siteUrl not set, auto-detected:", origin);
|
|
16
|
-
return origin;
|
|
17
|
-
}
|
|
18
|
-
export async function serverAuth(event) {
|
|
19
|
-
if (event.context._betterAuth)
|
|
20
|
-
return event.context._betterAuth;
|
|
6
|
+
let _auth = null;
|
|
7
|
+
export function serverAuth() {
|
|
8
|
+
if (_auth)
|
|
9
|
+
return _auth;
|
|
21
10
|
const runtimeConfig = useRuntimeConfig();
|
|
11
|
+
const siteUrl = runtimeConfig.public.siteUrl;
|
|
12
|
+
if (!siteUrl)
|
|
13
|
+
throw new Error("siteUrl must be configured. Set NUXT_PUBLIC_SITE_URL or configure in nuxt.config.");
|
|
22
14
|
const database = createDatabase();
|
|
23
15
|
const userConfig = createServerAuth({ runtimeConfig, db });
|
|
24
|
-
|
|
16
|
+
_auth = betterAuth({
|
|
25
17
|
...userConfig,
|
|
26
18
|
...database && { database },
|
|
27
19
|
secondaryStorage: createSecondaryStorage(),
|
|
28
20
|
secret: runtimeConfig.betterAuthSecret,
|
|
29
|
-
baseURL:
|
|
21
|
+
baseURL: siteUrl
|
|
30
22
|
});
|
|
31
|
-
return
|
|
23
|
+
return _auth;
|
|
32
24
|
}
|
|
@@ -2,7 +2,7 @@ import { createError } from "h3";
|
|
|
2
2
|
import { matchesUser } from "../../utils/match-user.js";
|
|
3
3
|
import { serverAuth } from "./auth.js";
|
|
4
4
|
export async function getUserSession(event) {
|
|
5
|
-
const auth =
|
|
5
|
+
const auth = serverAuth();
|
|
6
6
|
const session = await auth.api.getSession({ headers: event.headers });
|
|
7
7
|
return session;
|
|
8
8
|
}
|
package/dist/runtime/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { NitroRouteRules } from 'nitropack/types';
|
|
2
2
|
import type { AuthSession, AuthUser } from './types/augment.js';
|
|
3
3
|
export type { AuthSession, AuthUser, ServerAuthContext, UserSessionComposable } from './types/augment.js';
|
|
4
|
-
export type { Auth, InferSession, InferUser } from 'better-auth';
|
|
4
|
+
export type { Auth, InferPluginTypes, InferSession, InferUser } from 'better-auth';
|
|
5
5
|
export type AuthMode = 'guest' | 'user';
|
|
6
6
|
export type UserMatch<T> = {
|
|
7
7
|
[K in keyof T]?: T[K] | T[K][];
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onmax/nuxt-better-auth",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.2-alpha.
|
|
4
|
+
"version": "0.0.2-alpha.12",
|
|
5
5
|
"packageManager": "pnpm@10.15.1",
|
|
6
6
|
"description": "Nuxt module for Better Auth integration with NuxtHub, route protection, session management, and role-based access",
|
|
7
7
|
"author": "onmax",
|