@onmax/nuxt-better-auth 0.0.2-alpha.10 → 0.0.2-alpha.11
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 +47 -3
- 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/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
|
|
|
@@ -244,6 +257,9 @@ const module$1 = defineNuxtModule({
|
|
|
244
257
|
nuxt.options.alias["#nuxt-better-auth"] = resolver.resolve("./runtime/types/augment");
|
|
245
258
|
nuxt.options.alias["#auth/server"] = serverConfigPath;
|
|
246
259
|
nuxt.options.alias["#auth/client"] = clientConfigPath;
|
|
260
|
+
if (secondaryStorageEnabled && !nuxt.options.alias["hub:kv"]) {
|
|
261
|
+
throw new Error("[nuxt-better-auth] hub:kv not found. Ensure @nuxthub/core is loaded before this module and hub.kv is enabled.");
|
|
262
|
+
}
|
|
247
263
|
const secondaryStorageCode = secondaryStorageEnabled ? `import { kv } from '../hub/kv.mjs'
|
|
248
264
|
export function createSecondaryStorage() {
|
|
249
265
|
return {
|
|
@@ -254,6 +270,9 @@ export function createSecondaryStorage() {
|
|
|
254
270
|
}` : `export function createSecondaryStorage() { return undefined }`;
|
|
255
271
|
const secondaryStorageTemplate = addTemplate({ filename: "better-auth/secondary-storage.mjs", getContents: () => secondaryStorageCode, write: true });
|
|
256
272
|
nuxt.options.alias["#auth/secondary-storage"] = secondaryStorageTemplate.dst;
|
|
273
|
+
if (hasHubDb && !nuxt.options.alias["hub:db"]) {
|
|
274
|
+
throw new Error("[nuxt-better-auth] hub:db not found. Ensure @nuxthub/core is loaded before this module and hub.db is configured.");
|
|
275
|
+
}
|
|
257
276
|
const hubDialect = getHubDialect(hub) ?? "sqlite";
|
|
258
277
|
const databaseCode = hasHubDb ? `import { db, schema } from '../hub/db.mjs'
|
|
259
278
|
import { drizzleAdapter } from 'better-auth/adapters/drizzle'
|
|
@@ -311,6 +330,18 @@ declare module '#nuxt-better-auth' {
|
|
|
311
330
|
${hasHubDb ? `db: typeof import('hub:db')['db']` : ""}
|
|
312
331
|
}
|
|
313
332
|
}
|
|
333
|
+
|
|
334
|
+
// Augment the config module to use the extended ServerAuthContext
|
|
335
|
+
interface _AugmentedServerAuthContext {
|
|
336
|
+
runtimeConfig: RuntimeConfig
|
|
337
|
+
${hasHubDb ? `db: typeof import('hub:db')['db']` : "db: unknown"}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
declare module '@onmax/nuxt-better-auth/config' {
|
|
341
|
+
import type { BetterAuthOptions } from 'better-auth'
|
|
342
|
+
type ServerAuthConfig = Omit<BetterAuthOptions, 'database' | 'secret' | 'baseURL'>
|
|
343
|
+
export function defineServerAuth<T extends ServerAuthConfig>(config: (ctx: _AugmentedServerAuthContext) => T): (ctx: _AugmentedServerAuthContext) => T
|
|
344
|
+
}
|
|
314
345
|
`
|
|
315
346
|
});
|
|
316
347
|
addTypeTemplate({
|
|
@@ -325,19 +356,32 @@ declare module '#nuxt-better-auth' {
|
|
|
325
356
|
addTypeTemplate({
|
|
326
357
|
filename: "types/nuxt-better-auth-nitro.d.ts",
|
|
327
358
|
getContents: () => `
|
|
359
|
+
declare module 'nitropack' {
|
|
360
|
+
interface NitroRouteRules {
|
|
361
|
+
auth?: import('${resolver.resolve("./runtime/types")}').AuthMeta
|
|
362
|
+
}
|
|
363
|
+
interface NitroRouteConfig {
|
|
364
|
+
auth?: import('${resolver.resolve("./runtime/types")}').AuthMeta
|
|
365
|
+
}
|
|
366
|
+
}
|
|
328
367
|
declare module 'nitropack/types' {
|
|
329
368
|
interface NitroRouteRules {
|
|
330
369
|
auth?: import('${resolver.resolve("./runtime/types")}').AuthMeta
|
|
331
370
|
}
|
|
371
|
+
interface NitroRouteConfig {
|
|
372
|
+
auth?: import('${resolver.resolve("./runtime/types")}').AuthMeta
|
|
373
|
+
}
|
|
332
374
|
}
|
|
375
|
+
export {}
|
|
333
376
|
`
|
|
334
|
-
});
|
|
377
|
+
}, { nuxt: true, nitro: true, node: true });
|
|
335
378
|
nuxt.hook("builder:watch", async (_event, relativePath) => {
|
|
336
379
|
if (relativePath.includes("auth.config")) {
|
|
337
380
|
await updateTemplates({ filter: (t) => t.filename.includes("nuxt-better-auth") });
|
|
338
381
|
}
|
|
339
382
|
});
|
|
340
383
|
addServerImportsDir(resolver.resolve("./runtime/server/utils"));
|
|
384
|
+
addServerImports([{ name: "defineServerAuth", from: resolver.resolve("./runtime/config") }]);
|
|
341
385
|
addServerScanDir(resolver.resolve("./runtime/server/middleware"));
|
|
342
386
|
addServerHandler({ route: "/api/auth/**", handler: resolver.resolve("./runtime/server/api/auth/[...all]") });
|
|
343
387
|
addImportsDir(resolver.resolve("./runtime/app/composables"));
|
|
@@ -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/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.11",
|
|
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",
|