@onmax/nuxt-better-auth 0.0.2-alpha.24 → 0.0.2-alpha.26

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.d.mts CHANGED
@@ -3,7 +3,7 @@ import { Nuxt } from '@nuxt/schema';
3
3
  import { BetterAuthModuleOptions } from '../dist/runtime/config.js';
4
4
  export { BetterAuthModuleOptions, defineClientAuth, defineServerAuth } from '../dist/runtime/config.js';
5
5
  import { BetterAuthOptions } from 'better-auth';
6
- export { AppSession, Auth, AuthMeta, AuthMode, AuthRouteRules, AuthSession, AuthUser, InferSession, InferUser, RequireSessionOptions, ServerAuthContext, UserMatch } from '../dist/runtime/types.js';
6
+ export { AppSession, Auth, AuthActionError, AuthMeta, AuthMode, AuthRouteRules, AuthSession, AuthUser, InferSession, InferUser, RequireSessionOptions, ServerAuthContext, UserMatch } from '../dist/runtime/types.js';
7
7
 
8
8
  interface DefineServerAuthFn {
9
9
  (...args: unknown[]): unknown;
package/dist/module.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onmax/nuxt-better-auth",
3
- "version": "0.0.2-alpha.24",
3
+ "version": "0.0.2-alpha.26",
4
4
  "configKey": "auth",
5
5
  "compatibility": {
6
6
  "nuxt": ">=4.0.0"
package/dist/module.mjs CHANGED
@@ -10,7 +10,7 @@ import { randomBytes } from 'node:crypto';
10
10
  import { isCI, isTest } from 'std-env';
11
11
  export { defineClientAuth, defineServerAuth } from '../dist/runtime/config.js';
12
12
 
13
- const version = "0.0.2-alpha.24";
13
+ const version = "0.0.2-alpha.26";
14
14
 
15
15
  function resolveDatabaseProvider(input) {
16
16
  const enabledProviders = Object.entries(input.providers).filter(([_id, provider]) => provider.isEnabled?.(input.context) ?? true);
@@ -142,7 +142,6 @@ function setupRuntimeConfig(input) {
142
142
  if (!configuredSiteUrl && process.env.NUXT_PUBLIC_SITE_URL)
143
143
  nuxt.options.runtimeConfig.public.siteUrl = process.env.NUXT_PUBLIC_SITE_URL;
144
144
  nuxt.options.runtimeConfig.public.auth = defu(nuxt.options.runtimeConfig.public.auth, {
145
- redirects: { login: options.redirects?.login ?? "/login", guest: options.redirects?.guest ?? "/" },
146
145
  preserveRedirect: options.preserveRedirect ?? true,
147
146
  redirectQueryKey: options.redirectQueryKey ?? "redirect",
148
147
  useDatabase: databaseProvider !== "none",
@@ -588,7 +587,6 @@ const module$1 = defineNuxtModule({
588
587
  clientOnly: false,
589
588
  serverConfig: "server/auth.config",
590
589
  clientConfig: "app/auth.config",
591
- redirects: { login: "/login", guest: "/" },
592
590
  preserveRedirect: true,
593
591
  redirectQueryKey: "redirect",
594
592
  secondaryStorage: false
@@ -1,5 +1,6 @@
1
1
  import createAppAuthClient from "#auth/client";
2
2
  import { computed, nextTick, useNuxtApp, useRequestHeaders, useRequestURL, useRuntimeConfig, useState, watch } from "#imports";
3
+ import { normalizeAuthActionError } from "../internal/auth-action-error.js";
3
4
  let _sessionSignalListenerBound = false;
4
5
  let _client = null;
5
6
  function isRecord(value) {
@@ -77,16 +78,17 @@ export function useUserSession() {
77
78
  const clientWithUpdateUser = client;
78
79
  const result = await clientWithUpdateUser.updateUser(updates);
79
80
  if (result?.error) {
80
- if (typeof result.error === "string")
81
- throw new Error(result.error);
82
81
  if (result.error instanceof Error)
83
82
  throw result.error;
84
- if (typeof result.error === "object" && result.error && "message" in result.error && typeof result.error.message === "string")
85
- throw new Error(result.error.message);
86
- throw new Error("Failed to update user");
83
+ const normalizedError = normalizeAuthActionError(result.error);
84
+ throw new Error(normalizedError.message);
87
85
  }
88
86
  } catch (error) {
89
87
  user.value = previousUser;
88
+ if (!(error instanceof Error)) {
89
+ const normalizedError = normalizeAuthActionError(error);
90
+ throw new Error(normalizedError.message);
91
+ }
90
92
  throw error;
91
93
  }
92
94
  }
@@ -0,0 +1,2 @@
1
+ import type { AuthActionError } from '../../types.js';
2
+ export declare function normalizeAuthActionError(error: unknown): AuthActionError;
@@ -0,0 +1,34 @@
1
+ function isRecord(value) {
2
+ return Boolean(value && typeof value === "object");
3
+ }
4
+ function getMessage(value) {
5
+ if (value instanceof Error)
6
+ return value.message;
7
+ if (typeof value === "string")
8
+ return value;
9
+ if (isRecord(value) && typeof value.message === "string")
10
+ return value.message;
11
+ return "Request failed. Please try again.";
12
+ }
13
+ function getCode(value) {
14
+ if (!isRecord(value))
15
+ return void 0;
16
+ return typeof value.code === "string" ? value.code : void 0;
17
+ }
18
+ function getStatus(value) {
19
+ if (!isRecord(value))
20
+ return void 0;
21
+ if (typeof value.status === "number")
22
+ return value.status;
23
+ if (typeof value.statusCode === "number")
24
+ return value.statusCode;
25
+ return void 0;
26
+ }
27
+ export function normalizeAuthActionError(error) {
28
+ return {
29
+ message: getMessage(error),
30
+ code: getCode(error),
31
+ status: getStatus(error),
32
+ raw: error
33
+ };
34
+ }
@@ -1,10 +1,13 @@
1
1
  import type { ComputedRef, Ref } from 'vue';
2
+ import type { AuthActionError } from '../../types.js';
2
3
  export type UserAuthActionStatus = 'idle' | 'pending' | 'success' | 'error';
3
4
  export interface UserAuthActionHandle<TArgs extends unknown[], TResult> {
4
- execute: (...args: TArgs) => Promise<TResult>;
5
+ execute: (...args: TArgs) => Promise<void>;
5
6
  status: Ref<UserAuthActionStatus>;
6
7
  pending: ComputedRef<boolean>;
7
- error: Ref<unknown | null>;
8
+ data: Ref<TResult | null>;
9
+ error: Ref<AuthActionError | null>;
10
+ errorMessage: ComputedRef<string | null>;
8
11
  }
9
12
  export type ActionHandleFor<T> = T extends (...args: infer A) => Promise<infer R> ? UserAuthActionHandle<A, R> : never;
10
13
  export type ActionHandleMap<T> = {
@@ -1,4 +1,5 @@
1
1
  import { computed, ref } from "#imports";
2
+ import { normalizeAuthActionError } from "./auth-action-error.js";
2
3
  function isRecord(value) {
3
4
  return Boolean(value && typeof value === "object");
4
5
  }
@@ -11,34 +12,52 @@ function isErrorResult(value) {
11
12
  }
12
13
  function createActionHandle(getMethod) {
13
14
  const status = ref("idle");
15
+ const data = ref(null);
14
16
  const error = ref(null);
15
17
  const pending = computed(() => status.value === "pending");
18
+ const errorMessage = computed(() => error.value?.message ?? null);
16
19
  let latestCallId = 0;
17
- const execute = (async (...args) => {
20
+ const run = async (...args) => {
18
21
  const callId = ++latestCallId;
19
22
  status.value = "pending";
23
+ data.value = null;
20
24
  error.value = null;
21
25
  try {
22
26
  const result = await getMethod()(...args);
23
- if (callId !== latestCallId)
24
- return result;
25
27
  if (isErrorResult(result)) {
26
- status.value = "error";
27
- error.value = result.error;
28
- return result;
28
+ const normalizedError = normalizeAuthActionError(result.error);
29
+ if (callId === latestCallId) {
30
+ status.value = "error";
31
+ data.value = null;
32
+ error.value = normalizedError;
33
+ }
34
+ return;
29
35
  }
30
- status.value = "success";
31
- error.value = null;
32
- return result;
33
- } catch (err) {
36
+ if (callId === latestCallId) {
37
+ status.value = "success";
38
+ data.value = result;
39
+ error.value = null;
40
+ }
41
+ } catch (thrown) {
42
+ const normalizedError = normalizeAuthActionError(thrown);
34
43
  if (callId === latestCallId) {
35
44
  status.value = "error";
36
- error.value = err;
45
+ data.value = null;
46
+ error.value = normalizedError;
37
47
  }
38
- throw err;
39
48
  }
49
+ };
50
+ const execute = (async (...args) => {
51
+ await run(...args);
40
52
  });
41
- return { execute, status, pending, error };
53
+ return {
54
+ execute,
55
+ status,
56
+ pending,
57
+ data,
58
+ error,
59
+ errorMessage
60
+ };
42
61
  }
43
62
  export function createActionHandles(getTarget, targetName) {
44
63
  const handles = /* @__PURE__ */ new Map();
@@ -25,13 +25,13 @@ export default defineNuxtRouteMiddleware(async (to) => {
25
25
  const redirectTo = typeof auth === "object" ? auth.redirectTo : void 0;
26
26
  if (mode === "guest") {
27
27
  if (loggedIn.value)
28
- return navigateTo(redirectTo ?? config?.redirects?.guest ?? "/");
28
+ return navigateTo(redirectTo ?? "/");
29
29
  return;
30
30
  }
31
31
  if (!loggedIn.value) {
32
32
  const resolved = resolveLoginRedirect({
33
33
  route: to,
34
- loginTarget: redirectTo ?? config?.redirects?.login ?? "/login",
34
+ loginTarget: redirectTo ?? "/login",
35
35
  config
36
36
  });
37
37
  return resolved.external ? navigateTo(resolved.to, { external: true }) : navigateTo(resolved.to);
@@ -373,12 +373,6 @@ function getAccountActions(row) {
373
373
  <div class="config-header">
374
374
  <UIcon name="i-lucide-settings-2" class="size-4" /><span>Module</span>
375
375
  </div>
376
- <div class="config-row">
377
- <span class="config-label">Login</span><span class="font-mono">{{ configData.config.module?.redirects?.login }}</span>
378
- </div>
379
- <div class="config-row">
380
- <span class="config-label">Guest</span><span class="font-mono">{{ configData.config.module?.redirects?.guest }}</span>
381
- </div>
382
376
  <div class="config-row">
383
377
  <span class="config-label">DB</span><UBadge :color="configData.config.module?.databaseProvider === 'none' ? 'neutral' : 'success'" variant="subtle" size="sm">
384
378
  {{ configData.config.module?.databaseProvider === "nuxthub" ? "Hub" : "Off" }}
@@ -24,10 +24,6 @@ export interface BetterAuthModuleOptions {
24
24
  serverConfig?: string;
25
25
  /** Client config path relative to rootDir. Default: 'app/auth.config' */
26
26
  clientConfig?: string;
27
- redirects?: {
28
- login?: string;
29
- guest?: string;
30
- };
31
27
  /**
32
28
  * When redirecting unauthenticated users to the login route, append a query param
33
29
  * containing the originally requested path (for safe "return-to" redirects).
@@ -62,10 +58,6 @@ export interface BetterAuthModuleOptions {
62
58
  };
63
59
  }
64
60
  export interface AuthRuntimeConfig {
65
- redirects: {
66
- login: string;
67
- guest: string;
68
- };
69
61
  preserveRedirect: boolean;
70
62
  redirectQueryKey: string;
71
63
  useDatabase: boolean;
@@ -1,10 +1,6 @@
1
1
  declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<{
2
2
  config: {
3
3
  module: {
4
- redirects: {
5
- login?: string;
6
- guest?: string;
7
- };
8
4
  preserveRedirect: boolean;
9
5
  redirectQueryKey: string;
10
6
  secondaryStorage: boolean;
@@ -18,7 +18,6 @@ export default defineEventHandler(async (event) => {
18
18
  config: {
19
19
  // Module config (nuxt.config.ts)
20
20
  module: {
21
- redirects: publicAuth?.redirects || { login: "/login", guest: "/" },
22
21
  preserveRedirect: publicAuth?.preserveRedirect ?? true,
23
22
  redirectQueryKey: publicAuth?.redirectQueryKey ?? "redirect",
24
23
  secondaryStorage: privateAuth?.secondaryStorage ?? false,
@@ -2,6 +2,12 @@ import type { NitroRouteRules } from 'nitropack/types';
2
2
  import type { AuthUser, UserMatch } from './types/augment.js';
3
3
  export type { AppSession, AuthSession, AuthUser, RequireSessionOptions, ServerAuthContext, UserMatch, UserSessionComposable } from './types/augment.js';
4
4
  export type { Auth, InferPluginTypes, InferSessionFromClient as InferSession, InferUserFromClient as InferUser } from 'better-auth';
5
+ export interface AuthActionError {
6
+ message: string;
7
+ code?: string;
8
+ status?: number;
9
+ raw: unknown;
10
+ }
5
11
  export type AuthMode = 'guest' | 'user';
6
12
  export type AuthMeta = false | AuthMode | {
7
13
  only?: AuthMode;
package/dist/types.d.mts CHANGED
@@ -6,6 +6,6 @@ export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<
6
6
 
7
7
  export { type BetterAuthModuleOptions, type defineClientAuth, type defineServerAuth } from '../dist/runtime/config.js'
8
8
 
9
- export { type AppSession, type Auth, type AuthMeta, type AuthMode, type AuthRouteRules, type AuthSession, type AuthUser, type InferSession, type InferUser, type RequireSessionOptions, type ServerAuthContext, type UserMatch } from '../dist/runtime/types.js'
9
+ export { type AppSession, type Auth, type AuthActionError, type AuthMeta, type AuthMode, type AuthRouteRules, type AuthSession, type AuthUser, type InferSession, type InferUser, type RequireSessionOptions, type ServerAuthContext, type UserMatch } from '../dist/runtime/types.js'
10
10
 
11
11
  export { default } from './module.mjs'
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.24",
4
+ "version": "0.0.2-alpha.26",
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",