@onmax/nuxt-better-auth 0.0.2-alpha.12 → 0.0.2-alpha.13

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
@@ -4,7 +4,7 @@
4
4
  "compatibility": {
5
5
  "nuxt": ">=3.0.0"
6
6
  },
7
- "version": "0.0.2-alpha.12",
7
+ "version": "0.0.2-alpha.13",
8
8
  "builder": {
9
9
  "@nuxt/module-builder": "1.0.2",
10
10
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -312,7 +312,7 @@ declare module '#auth/secondary-storage' {
312
312
  export function createSecondaryStorage(): SecondaryStorage | undefined
313
313
  }
314
314
  `
315
- });
315
+ }, { nitro: true, node: true });
316
316
  addTypeTemplate({
317
317
  filename: "types/auth-database.d.ts",
318
318
  getContents: () => `
@@ -322,7 +322,7 @@ declare module '#auth/database' {
322
322
  export const db: unknown
323
323
  }
324
324
  `
325
- });
325
+ }, { nitro: true, node: true });
326
326
  addTypeTemplate({
327
327
  filename: "types/nuxt-better-auth-infer.d.ts",
328
328
  getContents: () => `
@@ -354,7 +354,7 @@ declare module '@onmax/nuxt-better-auth/config' {
354
354
  export function defineServerAuth<T extends ServerAuthConfig>(config: (ctx: _AugmentedServerAuthContext) => T): (ctx: _AugmentedServerAuthContext) => T
355
355
  }
356
356
  `
357
- });
357
+ }, { nuxt: true, nitro: true, node: true });
358
358
  addTypeTemplate({
359
359
  filename: "types/nuxt-better-auth-nitro.d.ts",
360
360
  getContents: () => `
@@ -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 (event) => {
5
5
  try {
6
- const auth = serverAuth();
6
+ const auth = serverAuth(event);
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 = serverAuth();
4
+ const auth = serverAuth(event);
5
5
  return auth.handler(toWebRequest(event));
6
6
  });
@@ -1,5 +1,7 @@
1
1
  import type { Auth } from 'better-auth';
2
+ import type { H3Event } from 'h3';
2
3
  import createServerAuth from '#auth/server';
3
4
  type AuthInstance = Auth<ReturnType<typeof createServerAuth>>;
4
- export declare function serverAuth(): AuthInstance;
5
+ /** Returns Better Auth instance. Pass event for accurate URL detection on first call. */
6
+ export declare function serverAuth(event?: H3Event): AuthInstance;
5
7
  export {};
@@ -2,15 +2,37 @@ 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 { getRequestURL } from "h3";
5
6
  import { useRuntimeConfig } from "nitropack/runtime";
6
7
  let _auth = null;
7
- export function serverAuth() {
8
+ function validateURL(url) {
9
+ try {
10
+ return new URL(url).origin;
11
+ } catch {
12
+ throw new Error(`Invalid siteUrl: "${url}". Must be a valid URL.`);
13
+ }
14
+ }
15
+ function getBaseURL(event) {
16
+ const config = useRuntimeConfig();
17
+ if (config.public.siteUrl && typeof config.public.siteUrl === "string")
18
+ return validateURL(config.public.siteUrl);
19
+ if (event)
20
+ return getRequestURL(event).origin;
21
+ if (process.env.VERCEL_URL)
22
+ return validateURL(`https://${process.env.VERCEL_URL}`);
23
+ if (process.env.CF_PAGES_URL)
24
+ return validateURL(`https://${process.env.CF_PAGES_URL}`);
25
+ if (process.env.URL)
26
+ return validateURL(process.env.URL.startsWith("http") ? process.env.URL : `https://${process.env.URL}`);
27
+ if (import.meta.dev)
28
+ return "http://localhost:3000";
29
+ throw new Error("siteUrl required. Set NUXT_PUBLIC_SITE_URL.");
30
+ }
31
+ export function serverAuth(event) {
8
32
  if (_auth)
9
33
  return _auth;
10
34
  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.");
35
+ const siteUrl = getBaseURL(event);
14
36
  const database = createDatabase();
15
37
  const userConfig = createServerAuth({ runtimeConfig, db });
16
38
  _auth = betterAuth({
@@ -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 = serverAuth();
5
+ const auth = serverAuth(event);
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.12",
4
+ "version": "0.0.2-alpha.13",
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",