@nuxtjs/better-auth 0.3.4 → 0.3.6

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.4",
3
+ "version": "0.3.6",
4
4
  "configKey": "auth",
5
5
  "compatibility": {
6
6
  "nuxt": ">=4.0.0"
package/dist/module.mjs CHANGED
@@ -10,9 +10,10 @@ import { defu } from 'defu';
10
10
  import { toRouteMatcher, createRouter } from 'radix3';
11
11
  import { randomBytes } from 'node:crypto';
12
12
  import { isCI, isTest } from 'std-env';
13
+ import { isInternalAssetPath } from '../dist/runtime/internal/auth-route-rules.js';
13
14
  export { defineClientAuth, defineServerAuth } from '../dist/runtime/config.js';
14
15
 
15
- const version = "0.3.4";
16
+ const version = "0.3.6";
16
17
 
17
18
  const CONFIG_EXTENSIONS = [".ts", ".js", ".mts", ".cts", ".mjs", ".cjs"];
18
19
  const CONFIG_EXTENSION_RE = /\.[cm]?[jt]s$/;
@@ -331,8 +332,7 @@ function registerPrepareTypesHook(input) {
331
332
  nodeTsConfig.compilerOptions.paths ||= {};
332
333
  const projectReferenceTypePaths = [
333
334
  join(nuxt.options.buildDir, "types/nitro-imports.d.ts"),
334
- join(nuxt.options.buildDir, "types/auth-database.d.ts"),
335
- join(nuxt.options.buildDir, "types/auth-schema.d.ts")
335
+ join(nuxt.options.buildDir, "types/auth-database.d.ts")
336
336
  ];
337
337
  if (hasHubDb)
338
338
  projectReferenceTypePaths.push(join(nuxt.options.buildDir, "hub/db.d.ts"));
@@ -341,7 +341,8 @@ function registerPrepareTypesHook(input) {
341
341
  "#auth/server": nuxt.options.alias["#auth/server"],
342
342
  "#auth/client": nuxt.options.alias["#auth/client"],
343
343
  "#auth/database": nuxt.options.alias["#auth/database"],
344
- "#auth/schema": nuxt.options.alias["#auth/schema"],
344
+ // An exact .mjs path bypasses declaration lookup in TypeScript paths.
345
+ "#auth/schema": join(nuxt.options.buildDir, "better-auth/schema"),
345
346
  "#auth/route-rules": nuxt.options.alias["#auth/route-rules"]
346
347
  };
347
348
  for (const [key, value] of Object.entries(exactNodeAliases)) {
@@ -933,6 +934,36 @@ export const verification = getGeneratedTable('verification')
933
934
  export const schema = { ...generatedSchema, user, session, account, verification }
934
935
  `;
935
936
  }
937
+ function buildSchemaExportTypes(hasHubDb, hubDialect) {
938
+ if (!hasHubDb) {
939
+ return `export declare const user: undefined
940
+ export declare const session: undefined
941
+ export declare const account: undefined
942
+ export declare const verification: undefined
943
+ export declare const schema: undefined
944
+ `;
945
+ }
946
+ return `export * from './schema.${hubDialect}.js'
947
+
948
+ type GeneratedSchema = typeof import('./schema.${hubDialect}.js')
949
+ type GeneratedTable<Name extends string> = Name extends keyof GeneratedSchema
950
+ ? GeneratedSchema[Name]
951
+ : \`\${Name}s\` extends keyof GeneratedSchema
952
+ ? GeneratedSchema[\`\${Name}s\`]
953
+ : undefined
954
+
955
+ export declare const user: GeneratedTable<'user'>
956
+ export declare const session: GeneratedTable<'session'>
957
+ export declare const account: GeneratedTable<'account'>
958
+ export declare const verification: GeneratedTable<'verification'>
959
+ export declare const schema: GeneratedSchema & {
960
+ user: typeof user
961
+ session: typeof session
962
+ account: typeof account
963
+ verification: typeof verification
964
+ }
965
+ `;
966
+ }
936
967
  function buildAuthRouteRulesCode(authRouteRules) {
937
968
  return `export const authRouteRules = ${JSON.stringify(authRouteRules, null, 2)}
938
969
  `;
@@ -991,6 +1022,8 @@ function assertSafeAuthRouteRules(routeRules) {
991
1022
  for (const path of collectRouteRulePaths(patterns, ""))
992
1023
  paths.add(path);
993
1024
  const conflicts = [...paths].flatMap((path) => {
1025
+ if (isInternalAssetPath(path))
1026
+ return [];
994
1027
  const matches = matcher.matchAll(path);
995
1028
  const effectiveRule = defu({}, ...matches.reverse());
996
1029
  if (effectiveRule.auth === void 0 || effectiveRule.auth === false)
@@ -1162,24 +1195,6 @@ declare module '#auth/database' {
1162
1195
  export function createDatabase(event?: import('${h3TypesPath}').H3Event): BetterAuthOptions['database']
1163
1196
  export const db: ${hasHubDb ? `typeof import('@nuxthub/db')['db']` : "undefined"}
1164
1197
  }
1165
- `
1166
- }, { nitro: true });
1167
- addTypeTemplate({
1168
- filename: "types/auth-schema.d.ts",
1169
- getContents: () => `
1170
- declare module '#auth/schema' {
1171
- export const user: any
1172
- export const session: any
1173
- export const account: any
1174
- export const verification: any
1175
- export const schema: {
1176
- user: any
1177
- session: any
1178
- account: any
1179
- verification: any
1180
- [key: string]: any
1181
- } | undefined
1182
- }
1183
1198
  `
1184
1199
  }, { nitro: true });
1185
1200
  addTypeTemplate({
@@ -1187,7 +1202,6 @@ declare module '#auth/schema' {
1187
1202
  getContents: () => `
1188
1203
  /// <reference path="./nitro-imports.d.ts" />
1189
1204
  /// <reference path="./auth-database.d.ts" />
1190
- /// <reference path="./auth-schema.d.ts" />
1191
1205
  ${hasHubDb ? '/// <reference path="../hub/db.d.ts" />' : ""}
1192
1206
 
1193
1207
  export {}
@@ -1676,6 +1690,11 @@ const module$1 = defineNuxtModule({
1676
1690
  write: true
1677
1691
  });
1678
1692
  nuxt.options.alias["#auth/schema"] = schemaTemplate.dst;
1693
+ addTemplate({
1694
+ filename: "better-auth/schema.d.ts",
1695
+ getContents: () => buildSchemaExportTypes(setup.database.hasHubDb, setup.database.buildContext?.hubDialect ?? "sqlite"),
1696
+ write: true
1697
+ });
1679
1698
  if (setup.schemaGeneration)
1680
1699
  await ensureSchemaBootstrap(schemaTemplate.dst, setup.database.buildContext?.hubDialect ?? "sqlite");
1681
1700
  }
@@ -1,5 +1,6 @@
1
1
  import createAppAuthClient from "#auth/client";
2
2
  import { useRequestURL, useRuntimeConfig } from "#imports";
3
+ import { resolveAuthBaseURL } from "../../internal/auth-base-url.js";
3
4
  import { createVueSafeAuthProxy } from "../internal/vue-safe-auth-proxy.js";
4
5
  let _client = null;
5
6
  let _clientFacade = null;
@@ -27,7 +28,9 @@ export function useRawAuthClient() {
27
28
  const requestURL = useRequestURL();
28
29
  const configuredSiteUrl = runtimeConfig.public.siteUrl;
29
30
  const siteUrl = typeof configuredSiteUrl === "string" && configuredSiteUrl ? configuredSiteUrl : requestURL.origin;
30
- return getClient(siteUrl);
31
+ const clientOnly = runtimeConfig.public.auth?.clientOnly;
32
+ const appBaseURL = runtimeConfig.app?.baseURL;
33
+ return getClient(clientOnly ? siteUrl : resolveAuthBaseURL(siteUrl, appBaseURL));
31
34
  }
32
35
  export function useAuthClient() {
33
36
  const rawClient = useRawAuthClient();
@@ -0,0 +1,2 @@
1
+ /** Better Auth treats a path in baseURL as the complete auth endpoint path. */
2
+ export declare function resolveAuthBaseURL(siteUrl: string, appBaseURL?: string): string;
@@ -0,0 +1,5 @@
1
+ import { joinURL } from "ufo";
2
+ export function resolveAuthBaseURL(siteUrl, appBaseURL) {
3
+ const origin = new URL(siteUrl).origin;
4
+ return appBaseURL && appBaseURL !== "/" ? joinURL(origin, appBaseURL, "/api/auth") : origin;
5
+ }
@@ -1,4 +1,5 @@
1
1
  import type { AuthRouteRules } from '../types.js';
2
+ export declare function isInternalAssetPath(path: string): boolean;
2
3
  export declare function normalizeAuthRoutePath(path: string, baseURL?: string): string;
3
4
  export declare function normalizeAuthRouteRule(rule: unknown): AuthRouteRules['auth'];
4
5
  export declare function shouldSkipAuthRouteRules(path: string): boolean;
@@ -1,22 +1,21 @@
1
1
  import { withoutBase } from "ufo";
2
+ const internalAssetPaths = ["/_fonts", "/_ipx", "/_nuxt", "/api/_nuxt_icon"];
3
+ export function isInternalAssetPath(path) {
4
+ const pathname = path.split(/[?#]/, 1)[0] || "/";
5
+ return internalAssetPaths.some((prefix) => pathname === prefix || pathname.startsWith(`${prefix}/`));
6
+ }
2
7
  const internalRouteRuleAuthPaths = /* @__PURE__ */ new Set([
3
- "/_ipx",
4
- "/_nuxt",
5
8
  "/__better-auth-devtools",
6
9
  "/__nuxt_devtools__",
7
10
  "/__nuxt_error",
8
11
  "/__nuxt_vite_node__",
9
12
  "/api/_better-auth",
10
- "/api/_nuxt_icon",
11
13
  "/api/auth"
12
14
  ]);
13
15
  const internalRouteRuleAuthPrefixes = [
14
- "/_ipx/",
15
- "/_nuxt/",
16
16
  "/__nuxt_devtools__/",
17
17
  "/__nuxt_vite_node__/",
18
18
  "/api/_better-auth/",
19
- "/api/_nuxt_icon/",
20
19
  "/api/auth/"
21
20
  ];
22
21
  export function normalizeAuthRoutePath(path, baseURL = "/") {
@@ -30,5 +29,5 @@ export function normalizeAuthRouteRule(rule) {
30
29
  }
31
30
  export function shouldSkipAuthRouteRules(path) {
32
31
  const pathname = path.split(/[?#]/, 1)[0] || "/";
33
- return internalRouteRuleAuthPaths.has(pathname) || internalRouteRuleAuthPrefixes.some((prefix) => pathname.startsWith(prefix));
32
+ return isInternalAssetPath(pathname) || internalRouteRuleAuthPaths.has(pathname) || internalRouteRuleAuthPrefixes.some((prefix) => pathname.startsWith(prefix));
34
33
  }
@@ -1,3 +1,5 @@
1
+ import type { Account, Session, User } from 'better-auth/types';
2
+ import type { AnyColumn, Table } from 'drizzle-orm';
1
3
  import { z } from 'zod';
2
4
  export declare const paginationQuerySchema: z.ZodObject<{
3
5
  page: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
@@ -6,3 +8,8 @@ export declare const paginationQuerySchema: z.ZodObject<{
6
8
  }, z.core.$strip>;
7
9
  export type PaginationQuery = z.infer<typeof paginationQuerySchema>;
8
10
  export declare function sanitizeSearchPattern(search: string): string;
11
+ export type DevtoolsSchema = {
12
+ user?: Table & Record<keyof User, AnyColumn>;
13
+ session?: Table & Record<keyof Session, AnyColumn>;
14
+ account?: Table & Record<keyof Account, AnyColumn>;
15
+ } | undefined;
@@ -3,6 +3,7 @@ import { withoutProtocol } from "ufo";
3
3
  import { createDatabase, db } from "#auth/database";
4
4
  import createServerAuth from "#auth/server";
5
5
  import { getRequestHost, getRequestProtocol, useRuntimeConfig } from "../internal/nitro-compat.js";
6
+ import { resolveAuthBaseURL } from "../../internal/auth-base-url.js";
6
7
  import { resolveCustomSecondaryStorageRequirement } from "./custom-secondary-storage.js";
7
8
  const _authCache = /* @__PURE__ */ new Map();
8
9
  const requestAuthKey = Symbol.for("nuxt-better-auth.requestAuth");
@@ -254,7 +255,7 @@ export function serverAuth(event) {
254
255
  ...rateLimit ? { rateLimit } : {},
255
256
  ...database ? { database } : {},
256
257
  secret: betterAuthSecret,
257
- baseURL: siteUrl,
258
+ baseURL: resolveAuthBaseURL(siteUrl, runtimeConfig.app?.baseURL),
258
259
  trustedOrigins
259
260
  };
260
261
  const auth = betterAuth(authOptions);
@@ -8,10 +8,6 @@ declare module '#imports' {
8
8
  export function useRuntimeConfig(): any
9
9
  }
10
10
 
11
- declare module '#auth/schema' {
12
- export const schema: any
13
- }
14
-
15
11
  declare module '#auth/server' {
16
12
  const createServerAuth: any
17
13
  export default createServerAuth
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nuxtjs/better-auth",
3
3
  "type": "module",
4
- "version": "0.3.4",
4
+ "version": "0.3.6",
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",
@@ -94,6 +94,7 @@
94
94
  "@libsql/client": "^0.17.4",
95
95
  "@libsql/linux-x64-gnu": "0.5.29",
96
96
  "@nuxt/devtools": "^3.4.1",
97
+ "@nuxt/fonts": "0.14.0",
97
98
  "@nuxt/module-builder": "^1.0.3",
98
99
  "@nuxt/schema": "^4.5.2",
99
100
  "@nuxt/test-utils": "^4.1.0",