@nuxtjs/better-auth 0.3.12 → 0.3.14

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuxtjs/better-auth",
3
- "version": "0.3.12",
3
+ "version": "0.3.14",
4
4
  "configKey": "auth",
5
5
  "compatibility": {
6
6
  "nuxt": ">=4.0.0"
package/dist/module.mjs CHANGED
@@ -13,7 +13,7 @@ import { isCI, isTest } from 'std-env';
13
13
  import { isInternalAssetPath } from '../dist/runtime/internal/auth-route-rules.js';
14
14
  export { defineClientAuth, defineServerAuth } from '../dist/runtime/config.js';
15
15
 
16
- const version = "0.3.12";
16
+ const version = "0.3.14";
17
17
 
18
18
  const CONFIG_EXTENSIONS = [".ts", ".js", ".mts", ".cts", ".mjs", ".cjs"];
19
19
  const CONFIG_EXTENSION_RE = /\.[cm]?[jt]s$/;
@@ -966,7 +966,8 @@ function getCloudflareContext(event) {
966
966
 
967
967
  function resolveHyperdrive(event) {
968
968
  const cloudflareEnv = getCloudflareContext(event)?.env
969
- return cloudflareEnv?.POSTGRES || process.env.POSTGRES || globalThis.__env__?.POSTGRES || globalThis.POSTGRES
969
+ const runtimeEnv = event?.req?.runtime?.cloudflare?.env
970
+ return cloudflareEnv?.POSTGRES || runtimeEnv?.POSTGRES || process.env.POSTGRES || globalThis.__env__?.POSTGRES || globalThis.POSTGRES
970
971
  }
971
972
 
972
973
  function scheduleCleanup(event, cleanup) {
@@ -1008,6 +1009,11 @@ function registerClientCleanup(event, client) {
1008
1009
  response.once('close', cleanup)
1009
1010
  }
1010
1011
 
1012
+ // Hyperdrive clients belong to one request; any other database is shared by the whole process.
1013
+ export function isRequestScopedDatabase(event) {
1014
+ return Boolean(resolveHyperdrive(event)?.connectionString)
1015
+ }
1016
+
1011
1017
  export function createDatabase(event) {
1012
1018
  const hyperdrive = resolveHyperdrive(event)
1013
1019
  if (!hyperdrive?.connectionString)
@@ -1045,10 +1051,12 @@ import * as schema from './schema.${input.hubDialect}.mjs'
1045
1051
  import { drizzleAdapter } from 'better-auth/adapters/drizzle'
1046
1052
  const rawDialect = '${input.hubDialect}'
1047
1053
  const dialect = rawDialect === 'postgresql' ? 'pg' : rawDialect
1054
+ export function isRequestScopedDatabase() { return false }
1048
1055
  export function createDatabase() { return drizzleAdapter(db, { provider: dialect, schema, usePlural: ${input.usePlural}, camelCase: ${input.camelCase} }) }
1049
1056
  export { db }`;
1050
1057
  }
1051
- return `export function createDatabase() { return undefined }
1058
+ return `export function isRequestScopedDatabase() { return false }
1059
+ export function createDatabase() { return undefined }
1052
1060
  export const db = undefined`;
1053
1061
  }
1054
1062
  function buildSchemaExportCode(hasHubDb, hubDialect, modelNames = {}) {
@@ -1330,6 +1338,7 @@ type AvailableRouterMethod<_ReqT extends NitroFetchRequest> = 'get' | 'head' | '
1330
1338
  declare module '#auth/database' {
1331
1339
  import type { BetterAuthOptions } from 'better-auth'
1332
1340
  export function createDatabase(event?: import('${h3TypesPath}').H3Event): BetterAuthOptions['database']
1341
+ export function isRequestScopedDatabase(event?: import('${h3TypesPath}').H3Event): boolean
1333
1342
  export const db: ${hasHubDb ? `typeof import('@nuxthub/db')['db']` : "undefined"}
1334
1343
  }
1335
1344
  `
@@ -1916,7 +1925,7 @@ const module$1 = defineNuxtModule({
1916
1925
  const finishSetupOnce = () => {
1917
1926
  setupPromise ||= finishSetup().catch((error) => {
1918
1927
  if (error instanceof Diagnostic)
1919
- throw new Error(formatDiagnostic(error), { cause: error });
1928
+ throw new TypeError(formatDiagnostic(error), { cause: error });
1920
1929
  throw error;
1921
1930
  });
1922
1931
  return setupPromise;
@@ -78,10 +78,30 @@ export function toWebRequest(event) {
78
78
  headers: req?.headers ? Object.fromEntries(Object.entries(req.headers).flatMap(([key, value]) => value === void 0 ? [] : [[key, Array.isArray(value) ? value.join(", ") : value]])) : void 0,
79
79
  duplex: "half"
80
80
  };
81
- if (req?.method !== "GET" && req?.method !== "HEAD")
82
- init.body = req;
81
+ if (req && req.method !== "GET" && req.method !== "HEAD")
82
+ init.body = requestBody(req);
83
83
  return new Request(getRequestURL(event), init);
84
84
  }
85
+ const h3RawBody = Symbol.for("h3RawBody");
86
+ function requestBody(req) {
87
+ const rawBody = req[h3RawBody];
88
+ if (rawBody) {
89
+ return new ReadableStream({
90
+ async start(controller) {
91
+ const bytes = await rawBody;
92
+ if (bytes?.length)
93
+ controller.enqueue(bytes);
94
+ controller.close();
95
+ }
96
+ });
97
+ }
98
+ const body = req.rawBody ?? req.body;
99
+ if (body === void 0 || body === null)
100
+ return "__unenv__" in req ? void 0 : req;
101
+ if (body.constructor === Object)
102
+ return JSON.stringify(body);
103
+ return body;
104
+ }
85
105
  export async function readBody(event) {
86
106
  const request = toWebRequest(event);
87
107
  const contentType = request.headers.get("content-type") || "";
@@ -1,5 +1,5 @@
1
1
  import { betterAuth, env } from "better-auth";
2
- import { createDatabase, db } from "#auth/database";
2
+ import { createDatabase, db, isRequestScopedDatabase } from "#auth/database";
3
3
  import createServerAuth from "#auth/server";
4
4
  import { getConfiguredAuthSecret, getDerivedAuthSecret, hasExplicitAuthSecret, prepareAuthSecret } from "../internal/auth-secret.js";
5
5
  import { getRequestHost, getRequestProtocol, useRuntimeConfig } from "../internal/nitro-compat.js";
@@ -248,6 +248,15 @@ export function serverAuth(event) {
248
248
  const requestContext = event ? getRequestAuthContext(event) : void 0;
249
249
  if (requestContext?.[requestAuthKey])
250
250
  return requestContext[requestAuthKey];
251
+ const requestScopedDatabase = isRequestScopedDatabase(event);
252
+ if (!requestScopedDatabase) {
253
+ const cached = _authCache.get(cacheKey);
254
+ if (cached) {
255
+ if (requestContext)
256
+ requestContext[requestAuthKey] = cached;
257
+ return cached;
258
+ }
259
+ }
251
260
  userConfig ??= createServerAuth({ runtimeConfig, db, requestOrigin });
252
261
  const usesDerivedSecret = derived.available && userConfig.secrets === void 0 && !hasExplicitAuthSecret(runtimeConfig);
253
262
  const derivedSecretMissing = usesDerivedSecret && !derived.secret;
@@ -276,15 +285,7 @@ export function serverAuth(event) {
276
285
  console.warn('[nuxt-better-auth] rateLimit.storage: "secondary-storage" requires secondaryStorage. Falling back to process-local memory. Set rateLimit.storage to "database" or provide rateLimit.customStorage for shared limits.');
277
286
  }
278
287
  const rateLimit = useMemoryRateLimit ? { ...userConfig.rateLimit, storage: "memory" } : userConfig.rateLimit;
279
- const cacheable = !database && !derivedSecretMissing;
280
- if (cacheable) {
281
- const cached = _authCache.get(cacheKey);
282
- if (cached) {
283
- if (requestContext)
284
- requestContext[requestAuthKey] = cached;
285
- return cached;
286
- }
287
- }
288
+ const cacheable = !requestScopedDatabase && !derivedSecretMissing;
288
289
  const authOptions = {
289
290
  ...userConfig,
290
291
  ...userConfig.plugins ? { plugins: [...userConfig.plugins] } : {},
@@ -1,6 +1,7 @@
1
1
  declare module '#auth/database' {
2
2
  export const db: any
3
3
  export function createDatabase(...args: any[]): any
4
+ export function isRequestScopedDatabase(...args: any[]): boolean
4
5
  }
5
6
 
6
7
  declare module '#imports' {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nuxtjs/better-auth",
3
3
  "type": "module",
4
- "version": "0.3.12",
4
+ "version": "0.3.14",
5
5
  "description": "Nuxt module for Better Auth integration with NuxtHub, route protection, session management, and role-based access",
6
6
  "author": "onmax",
7
7
  "license": "MIT",
@@ -61,7 +61,7 @@
61
61
  "peerDependencies": {
62
62
  "@nuxt/test-utils": "^4.1.0",
63
63
  "@nuxthub/core": ">=0.10.5",
64
- "auth": "1.7.12",
64
+ "auth": "1.7.13",
65
65
  "better-auth": ">=1.7.3 <2"
66
66
  },
67
67
  "peerDependenciesMeta": {
@@ -93,20 +93,21 @@
93
93
  "zod": "^4.4.3"
94
94
  },
95
95
  "devDependencies": {
96
- "@antfu/eslint-config": "^9.3.0",
96
+ "@antfu/eslint-config": "^10.0.0",
97
97
  "@libsql/client": "^0.18.0",
98
98
  "@libsql/linux-x64-gnu": "0.5.29",
99
99
  "@nuxt/devtools": "^3.4.1",
100
- "@nuxt/fonts": "1.0.1",
100
+ "@nuxt/fonts": "1.2.0",
101
101
  "@nuxt/module-builder": "^1.0.3",
102
102
  "@nuxt/schema": "^4.6.0",
103
103
  "@nuxt/test-utils": "^4.3.3",
104
104
  "@nuxthub/core": "^0.10.8",
105
105
  "@pinia/nuxt": "^1.0.2",
106
106
  "@types/better-sqlite3": "^9.6.0",
107
+ "@typescript/typescript6": "6.0.2",
107
108
  "@vue/test-utils": "^2.4.6",
108
- "auth": "1.7.12",
109
- "better-auth": "1.7.12",
109
+ "auth": "1.7.13",
110
+ "better-auth": "1.7.13",
110
111
  "better-call": "1.4.4",
111
112
  "better-sqlite3": "^13.0.0",
112
113
  "drizzle-kit": "^0.31.10",
@@ -116,15 +117,16 @@
116
117
  "nitropack": "2.13.5",
117
118
  "nuxt": "^4.6.0",
118
119
  "pinia": "^4.0.3",
119
- "pkg-pr-new": "0.0.91",
120
+ "pkg-pr-new": "0.0.92",
120
121
  "playwright-core": "1.64.1",
121
122
  "postgres": "^3.4.9",
122
- "typescript": "~5.9.3",
123
+ "typescript": "~7.0.0",
124
+ "typescript-next": "npm:typescript@~7.0.0",
123
125
  "unstorage": "^1.17.5",
124
126
  "vitest": "^5.0.0",
125
127
  "vue": "3.5.44",
126
128
  "vue-tsc": "^3.3.10",
127
- "wrangler": "4.151.3",
129
+ "wrangler": "4.152.0",
128
130
  "yaml": "^2.9.0"
129
131
  },
130
132
  "scripts": {
@@ -135,11 +137,11 @@
135
137
  "dev:docs": "nuxi dev docs",
136
138
  "build:docs": "nuxi build docs",
137
139
  "deploy": "wrangler deploy",
138
- "lint": "eslint .",
140
+ "lint": "NODE_OPTIONS='--import=./scripts/typescript6-shim.mjs --experimental-loader=./scripts/typescript6-shim.mjs' eslint .",
139
141
  "lint:fix": "eslint . --fix",
140
- "typecheck": "vue-tsc --noEmit",
141
- "typecheck:runtime-server": "cd test/cases/plugins-type-inference && nuxi prepare && vue-tsc --noEmit --pretty false -p .nuxt/tsconfig.server.json",
142
- "typecheck:playground": "pnpm -C playground exec nuxi prepare && pnpm -C playground exec vue-tsc --noEmit -p .nuxt/tsconfig.app.json",
142
+ "typecheck": "NODE_OPTIONS='--import=./scripts/typescript6-shim.mjs --experimental-loader=./scripts/typescript6-shim.mjs' vue-tsc --noEmit",
143
+ "typecheck:runtime-server": "cd test/cases/plugins-type-inference && NODE_OPTIONS='--import=../../../scripts/typescript6-shim.mjs --experimental-loader=../../../scripts/typescript6-shim.mjs' nuxi prepare && NODE_OPTIONS='--import=../../../scripts/typescript6-shim.mjs --experimental-loader=../../../scripts/typescript6-shim.mjs' vue-tsc --noEmit --pretty false -p .nuxt/tsconfig.server.json",
144
+ "typecheck:playground": "NODE_OPTIONS=\"--import=$PWD/scripts/typescript6-shim.mjs --experimental-loader=$PWD/scripts/typescript6-shim.mjs\" pnpm -C playground exec nuxi prepare && NODE_OPTIONS=\"--import=$PWD/scripts/typescript6-shim.mjs --experimental-loader=$PWD/scripts/typescript6-shim.mjs\" pnpm -C playground exec vue-tsc --noEmit -p .nuxt/tsconfig.app.json",
143
145
  "test": "vitest run",
144
146
  "test:watch": "vitest watch",
145
147
  "test:runtime": "vitest run --config vitest.runtime.config.ts"