@onmax/nuxt-better-auth 0.0.2-alpha.25 → 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.25",
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.25";
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);
@@ -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();
@@ -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.25",
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",