@onmax/nuxt-better-auth 0.0.2-alpha.2 → 0.0.2-alpha.21

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.
@@ -1,10 +1,7 @@
1
+ import type { Auth } from 'better-auth';
1
2
  import type { H3Event } from 'h3';
2
- import { betterAuth } from 'better-auth';
3
- type AuthInstance = ReturnType<typeof betterAuth>;
4
- declare module 'h3' {
5
- interface H3EventContext {
6
- _betterAuth?: AuthInstance;
7
- }
8
- }
9
- export declare function serverAuth(event: H3Event): Promise<AuthInstance>;
3
+ import createServerAuth from '#auth/server';
4
+ type AuthInstance = Auth<ReturnType<typeof createServerAuth>>;
5
+ /** Returns Better Auth instance. Pass event for accurate URL detection on first call. */
6
+ export declare function serverAuth(event?: H3Event): AuthInstance;
10
7
  export {};
@@ -2,31 +2,80 @@ 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";
5
+ import { getRequestHost, getRequestProtocol } from "h3";
7
6
  import { useRuntimeConfig } from "nitropack/runtime";
8
- const logger = consola.withTag("nuxt-better-auth");
9
- function getBaseURL(event, siteUrl) {
10
- if (siteUrl)
11
- return siteUrl;
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;
7
+ import { withoutProtocol } from "ufo";
8
+ let _auth = null;
9
+ function validateURL(url) {
10
+ try {
11
+ return new URL(url).origin;
12
+ } catch {
13
+ throw new Error(`Invalid siteUrl: "${url}". Must be a valid URL.`);
14
+ }
17
15
  }
18
- export async function serverAuth(event) {
19
- if (event.context._betterAuth)
20
- return event.context._betterAuth;
16
+ function getNitroOrigin(e) {
17
+ const cert = process.env.NITRO_SSL_CERT;
18
+ const key = process.env.NITRO_SSL_KEY;
19
+ let host = process.env.NITRO_HOST || process.env.HOST;
20
+ let port;
21
+ if (import.meta.dev)
22
+ port = process.env.NITRO_PORT || process.env.PORT || "3000";
23
+ let protocol = cert && key || !import.meta.dev ? "https" : "http";
24
+ try {
25
+ if ((import.meta.dev || import.meta.prerender) && process.env.__NUXT_DEV__) {
26
+ const origin = JSON.parse(process.env.__NUXT_DEV__).proxy.url;
27
+ host = withoutProtocol(origin);
28
+ protocol = origin.includes("https") ? "https" : "http";
29
+ } else if ((import.meta.dev || import.meta.prerender) && process.env.NUXT_VITE_NODE_OPTIONS) {
30
+ const origin = JSON.parse(process.env.NUXT_VITE_NODE_OPTIONS).baseURL.replace("/__nuxt_vite_node__", "");
31
+ host = withoutProtocol(origin);
32
+ protocol = origin.includes("https") ? "https" : "http";
33
+ } else if (e) {
34
+ host = getRequestHost(e, { xForwardedHost: true }) || host;
35
+ protocol = getRequestProtocol(e, { xForwardedProto: true }) || protocol;
36
+ }
37
+ } catch {
38
+ }
39
+ if (!host)
40
+ return void 0;
41
+ if (host.includes(":") && !host.startsWith("[")) {
42
+ const hostParts = host.split(":");
43
+ port = hostParts.pop();
44
+ host = hostParts.join(":");
45
+ }
46
+ const portSuffix = port ? `:${port}` : "";
47
+ return `${protocol}://${host}${portSuffix}`;
48
+ }
49
+ function getBaseURL(event) {
50
+ const config = useRuntimeConfig();
51
+ if (config.public.siteUrl && typeof config.public.siteUrl === "string")
52
+ return validateURL(config.public.siteUrl);
53
+ const nitroOrigin = getNitroOrigin(event);
54
+ if (nitroOrigin)
55
+ return validateURL(nitroOrigin);
56
+ if (process.env.VERCEL_URL)
57
+ return validateURL(`https://${process.env.VERCEL_URL}`);
58
+ if (process.env.CF_PAGES_URL)
59
+ return validateURL(`https://${process.env.CF_PAGES_URL}`);
60
+ if (process.env.URL)
61
+ return validateURL(process.env.URL.startsWith("http") ? process.env.URL : `https://${process.env.URL}`);
62
+ if (import.meta.dev)
63
+ return "http://localhost:3000";
64
+ throw new Error("siteUrl required. Set NUXT_PUBLIC_SITE_URL.");
65
+ }
66
+ export function serverAuth(event) {
67
+ if (_auth)
68
+ return _auth;
21
69
  const runtimeConfig = useRuntimeConfig();
70
+ const siteUrl = getBaseURL(event);
22
71
  const database = createDatabase();
23
72
  const userConfig = createServerAuth({ runtimeConfig, db });
24
- event.context._betterAuth = betterAuth({
73
+ _auth = betterAuth({
25
74
  ...userConfig,
26
75
  ...database && { database },
27
76
  secondaryStorage: createSecondaryStorage(),
28
77
  secret: runtimeConfig.betterAuthSecret,
29
- baseURL: getBaseURL(event, runtimeConfig.public.siteUrl)
78
+ baseURL: siteUrl
30
79
  });
31
- return event.context._betterAuth;
80
+ return _auth;
32
81
  }
@@ -1,7 +1,8 @@
1
1
  import { createError } from "h3";
2
2
  import { matchesUser } from "../../utils/match-user.js";
3
+ import { serverAuth } from "./auth.js";
3
4
  export async function getUserSession(event) {
4
- const auth = await serverAuth(event);
5
+ const auth = serverAuth(event);
5
6
  const session = await auth.api.getSession({ headers: event.headers });
6
7
  return session;
7
8
  }
@@ -37,6 +37,6 @@ export interface UserSessionComposable {
37
37
  waitForSession: () => Promise<void>;
38
38
  signOut: (options?: {
39
39
  onSuccess?: () => void | Promise<void>;
40
- }) => Promise<unknown>;
40
+ }) => Promise<void>;
41
41
  updateUser: (updates: Partial<AuthUser>) => void;
42
42
  }
@@ -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,14 +1,22 @@
1
1
  {
2
2
  "name": "@onmax/nuxt-better-auth",
3
3
  "type": "module",
4
- "version": "0.0.2-alpha.2",
4
+ "version": "0.0.2-alpha.21",
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",
8
8
  "license": "MIT",
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/onmax/nuxt-better-auth"
11
+ "url": "https://github.com/nuxt-modules/better-auth"
12
+ },
13
+ "agents": {
14
+ "skills": [
15
+ {
16
+ "name": "nuxt-better-auth",
17
+ "path": "./skills/nuxt-better-auth"
18
+ }
19
+ ]
12
20
  },
13
21
  "exports": {
14
22
  ".": {
@@ -18,6 +26,10 @@
18
26
  "./config": {
19
27
  "types": "./dist/runtime/config.d.ts",
20
28
  "import": "./dist/runtime/config.js"
29
+ },
30
+ "./adapters/convex": {
31
+ "types": "./dist/runtime/adapters/convex.d.ts",
32
+ "import": "./dist/runtime/adapters/convex.js"
21
33
  }
22
34
  },
23
35
  "main": "./dist/module.mjs",
@@ -28,11 +40,15 @@
28
40
  ],
29
41
  "config": [
30
42
  "./dist/runtime/config.d.ts"
43
+ ],
44
+ "adapters/convex": [
45
+ "./dist/runtime/adapters/convex.d.ts"
31
46
  ]
32
47
  }
33
48
  },
34
49
  "files": [
35
- "dist"
50
+ "dist",
51
+ "skills"
36
52
  ],
37
53
  "scripts": {
38
54
  "prepack": "nuxt-module-build build",
@@ -41,7 +57,7 @@
41
57
  "dev:prepare": "nuxt-module-build build --stub && nuxi prepare playground",
42
58
  "dev:docs": "nuxi dev docs",
43
59
  "build:docs": "nuxi build docs",
44
- "release": "bumpp && git push --follow-tags",
60
+ "release": "bumpp --push --no-push-all",
45
61
  "lint": "eslint .",
46
62
  "lint:fix": "eslint . --fix",
47
63
  "typecheck": "vue-tsc --noEmit",
@@ -50,34 +66,40 @@
50
66
  "test:watch": "vitest watch"
51
67
  },
52
68
  "peerDependencies": {
53
- "@nuxthub/core": ">=0.10.0",
54
- "better-auth": ">=1.0.0"
69
+ "@nuxthub/core": ">=0.10.5",
70
+ "better-auth": ">=1.0.0",
71
+ "convex": ">=1.25.0"
55
72
  },
56
73
  "peerDependenciesMeta": {
57
74
  "@nuxthub/core": {
58
75
  "optional": true
76
+ },
77
+ "convex": {
78
+ "optional": true
59
79
  }
60
80
  },
61
81
  "dependencies": {
82
+ "@better-auth/cli": "^1.5.0-beta.3",
62
83
  "@nuxt/kit": "^4.2.2",
84
+ "@nuxt/ui": "^4.2.1",
63
85
  "defu": "^6.1.4",
64
86
  "jiti": "^2.4.2",
65
87
  "pathe": "^2.0.3",
66
- "radix3": "^1.1.2"
88
+ "radix3": "^1.1.2",
89
+ "std-env": "^3.10.0"
67
90
  },
68
91
  "devDependencies": {
69
92
  "@antfu/eslint-config": "^4.12.0",
70
- "@better-auth/cli": "^1.4.6",
71
93
  "@libsql/client": "^0.15.15",
72
94
  "@nuxt/devtools": "^3.1.1",
73
95
  "@nuxt/devtools-kit": "^3.1.1",
74
96
  "@nuxt/module-builder": "^1.0.2",
75
97
  "@nuxt/schema": "^4.2.2",
76
98
  "@nuxt/test-utils": "^3.21.0",
77
- "@nuxthub/core": "^0.10.3",
99
+ "@nuxthub/core": "^0.10.5",
78
100
  "@types/better-sqlite3": "^7.6.13",
79
101
  "@types/node": "latest",
80
- "better-auth": "^1.4.7",
102
+ "better-auth": "^1.5.0-beta.3",
81
103
  "better-sqlite3": "^11.9.1",
82
104
  "bumpp": "^10.3.2",
83
105
  "changelogen": "^0.6.2",
@@ -85,6 +107,7 @@
85
107
  "drizzle-kit": "^0.31.8",
86
108
  "drizzle-orm": "^0.38.4",
87
109
  "eslint": "^9.39.1",
110
+ "npm-agentskills": "https://pkg.pr.new/onmax/npm-agentskills@394499e",
88
111
  "nuxt": "^4.2.2",
89
112
  "tinyexec": "^1.0.2",
90
113
  "typescript": "~5.9.3",
@@ -95,9 +118,11 @@
95
118
  },
96
119
  "pnpm": {
97
120
  "onlyBuiltDependencies": [
121
+ "@parcel/watcher",
98
122
  "better-sqlite3",
99
123
  "esbuild",
100
- "@parcel/watcher"
124
+ "sharp",
125
+ "workerd"
101
126
  ],
102
127
  "patchedDependencies": {
103
128
  "@peculiar/x509@1.14.2": "patches/@peculiar__x509@1.14.2.patch",