@onmax/nuxt-better-auth 0.0.2-alpha.31 → 0.0.2-alpha.32

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.
Files changed (39) hide show
  1. package/README.md +60 -17
  2. package/dist/module.json +2 -2
  3. package/dist/module.mjs +580 -261
  4. package/dist/runtime/app/components/BetterAuthState.d.vue.ts +4 -4
  5. package/dist/runtime/app/components/BetterAuthState.vue +1 -1
  6. package/dist/runtime/app/components/BetterAuthState.vue.d.ts +4 -4
  7. package/dist/runtime/app/composables/useAuthClient.d.ts +9 -0
  8. package/dist/runtime/app/composables/useAuthClient.js +34 -0
  9. package/dist/runtime/app/composables/useAuthClientAction.d.ts +1 -3
  10. package/dist/runtime/app/composables/useAuthClientAction.js +2 -2
  11. package/dist/runtime/app/composables/useAuthRequestFetch.d.ts +1 -1
  12. package/dist/runtime/app/composables/useSignIn.js +2 -2
  13. package/dist/runtime/app/composables/useSignUp.js +2 -2
  14. package/dist/runtime/app/composables/useUserSession.d.ts +5 -4
  15. package/dist/runtime/app/composables/useUserSession.js +62 -69
  16. package/dist/runtime/app/composables/useUserSessionState.d.ts +3 -0
  17. package/dist/runtime/app/composables/useUserSessionState.js +4 -0
  18. package/dist/runtime/app/internal/session-fetch.d.ts +1 -1
  19. package/dist/runtime/app/internal/vue-safe-auth-proxy.d.ts +3 -0
  20. package/dist/runtime/app/internal/vue-safe-auth-proxy.js +68 -0
  21. package/dist/runtime/app/middleware/auth.global.js +5 -4
  22. package/dist/runtime/app/plugins/session.client.js +2 -1
  23. package/dist/runtime/composables.d.ts +11 -0
  24. package/dist/runtime/composables.js +9 -0
  25. package/dist/runtime/config.d.ts +4 -2
  26. package/dist/runtime/server/api/_better-auth/_schema.d.ts +1 -5
  27. package/dist/runtime/server/api/_better-auth/accounts.get.d.ts +2 -2
  28. package/dist/runtime/server/api/_better-auth/config.get.js +1 -1
  29. package/dist/runtime/server/api/_better-auth/sessions.get.d.ts +2 -2
  30. package/dist/runtime/server/api/_better-auth/users.get.d.ts +2 -2
  31. package/dist/runtime/server/middleware/route-access.js +1 -1
  32. package/dist/runtime/server/utils/auth.d.ts +13 -2
  33. package/dist/runtime/server/utils/auth.js +42 -16
  34. package/dist/runtime/server/utils/session.d.ts +3 -1
  35. package/dist/runtime/server/utils/session.js +175 -1
  36. package/dist/runtime/server/virtual-modules.d.ts +5 -0
  37. package/dist/runtime/types/augment.d.ts +1 -3
  38. package/dist/runtime/types.d.ts +1 -1
  39. package/package.json +28 -12
@@ -1,8 +1,8 @@
1
1
  declare var __VLS_1: {
2
- loggedIn: any;
3
- user: any;
4
- session: any;
5
- signOut: any;
2
+ loggedIn: boolean;
3
+ user: import("../../types.js").AuthUser | null;
4
+ session: import("../../types.js").AuthSession | null;
5
+ signOut: (options?: import("../composables/useUserSession.js").SignOutOptions) => Promise<void>;
6
6
  }, __VLS_3: {};
7
7
  type __VLS_Slots = {} & {
8
8
  default?: (props: typeof __VLS_1) => any;
@@ -1,5 +1,5 @@
1
1
  <script setup>
2
- import { useUserSession } from "#imports";
2
+ import { useUserSession } from "../composables/useUserSession";
3
3
  const { loggedIn, user, session, signOut, ready } = useUserSession();
4
4
  </script>
5
5
 
@@ -1,8 +1,8 @@
1
1
  declare var __VLS_1: {
2
- loggedIn: any;
3
- user: any;
4
- session: any;
5
- signOut: any;
2
+ loggedIn: boolean;
3
+ user: import("../../types.js").AuthUser | null;
4
+ session: import("../../types.js").AuthSession | null;
5
+ signOut: (options?: import("../composables/useUserSession.js").SignOutOptions) => Promise<void>;
6
6
  }, __VLS_3: {};
7
7
  type __VLS_Slots = {} & {
8
8
  default?: (props: typeof __VLS_1) => any;
@@ -0,0 +1,9 @@
1
+ import type { AppAuthClient } from '#nuxt-better-auth';
2
+ interface RuntimeFlags {
3
+ client: boolean;
4
+ server: boolean;
5
+ }
6
+ export declare function getAuthRuntimeFlags(): RuntimeFlags;
7
+ export declare function useRawAuthClient(): AppAuthClient | null;
8
+ export declare function useAuthClient(): AppAuthClient | null;
9
+ export {};
@@ -0,0 +1,34 @@
1
+ import createAppAuthClient from "#auth/client";
2
+ import { useRequestURL, useRuntimeConfig } from "#imports";
3
+ import { createVueSafeAuthProxy } from "../internal/vue-safe-auth-proxy.js";
4
+ let _client = null;
5
+ let _clientFacade = null;
6
+ export function getAuthRuntimeFlags() {
7
+ const globalFlags = globalThis.__NUXT_BETTER_AUTH_TEST_FLAGS__;
8
+ if (globalFlags)
9
+ return globalFlags;
10
+ return { client: Boolean(import.meta.client), server: Boolean(import.meta.server) };
11
+ }
12
+ function getClient(baseURL) {
13
+ if (!_client)
14
+ _client = createAppAuthClient(baseURL);
15
+ return _client;
16
+ }
17
+ function getClientFacade(client) {
18
+ if (!_clientFacade)
19
+ _clientFacade = createVueSafeAuthProxy(client);
20
+ return _clientFacade;
21
+ }
22
+ export function useRawAuthClient() {
23
+ const runtimeFlags = getAuthRuntimeFlags();
24
+ if (!runtimeFlags.client)
25
+ return null;
26
+ const runtimeConfig = useRuntimeConfig();
27
+ const requestURL = useRequestURL();
28
+ const siteUrl = typeof runtimeConfig.public.siteUrl === "string" ? runtimeConfig.public.siteUrl : requestURL.origin;
29
+ return getClient(siteUrl);
30
+ }
31
+ export function useAuthClient() {
32
+ const rawClient = useRawAuthClient();
33
+ return rawClient ? getClientFacade(rawClient) : null;
34
+ }
@@ -1,5 +1,3 @@
1
+ import type { AppAuthClient } from '#nuxt-better-auth';
1
2
  import type { UserAuthActionHandle } from '../internal/auth-action-handles.js';
2
- import type { UseUserSessionReturn } from './useUserSession.js';
3
- type AppAuthClient = NonNullable<UseUserSessionReturn['client']>;
4
3
  export declare function useAuthClientAction<TArgs extends unknown[], TResult>(select: (client: AppAuthClient) => (...args: TArgs) => Promise<TResult>): UserAuthActionHandle<TArgs, TResult>;
5
- export {};
@@ -1,10 +1,10 @@
1
- import { useUserSession } from "#imports";
2
1
  import { useAction } from "./useAction.js";
2
+ import { useAuthClient } from "./useAuthClient.js";
3
3
  export function useAuthClientAction(select) {
4
4
  if (typeof select !== "function")
5
5
  throw new TypeError("useAuthClientAction(select) requires a selector function");
6
6
  return useAction(async (...args) => {
7
- const { client } = useUserSession();
7
+ const client = useAuthClient();
8
8
  if (!client)
9
9
  throw new Error("Auth client is unavailable. This action can only run on client-side.");
10
10
  const method = select(client);
@@ -1,5 +1,5 @@
1
- import type { AuthApiEndpointMethod, AuthApiEndpointPath, AuthApiEndpointResponse } from '#nuxt-better-auth';
2
1
  import type { NitroFetchOptions } from 'nitropack/types';
2
+ import type { AuthApiEndpointMethod, AuthApiEndpointPath, AuthApiEndpointResponse } from '#nuxt-better-auth';
3
3
  import { useRequestFetch } from '#imports';
4
4
  type AuthRequestFetchExtractedMethod<Options> = Options extends undefined ? 'get' : Lowercase<Extract<Exclude<Options extends {
5
5
  method?: infer Method;
@@ -1,8 +1,8 @@
1
- import { useUserSession } from "#imports";
2
1
  import { createActionHandles } from "../internal/auth-action-handles.js";
2
+ import { useAuthActionNamespaces } from "./useUserSession.js";
3
3
  export function useSignIn(method) {
4
4
  if (method === void 0 || method === null)
5
5
  throw new TypeError("useSignIn(method) requires a sign-in method key");
6
- const handles = createActionHandles(() => useUserSession().signIn, "signIn");
6
+ const handles = createActionHandles(() => useAuthActionNamespaces().signIn, "signIn");
7
7
  return handles[method];
8
8
  }
@@ -1,8 +1,8 @@
1
- import { useUserSession } from "#imports";
2
1
  import { createActionHandles } from "../internal/auth-action-handles.js";
2
+ import { useAuthActionNamespaces } from "./useUserSession.js";
3
3
  export function useSignUp(method) {
4
4
  if (method === void 0 || method === null)
5
5
  throw new TypeError("useSignUp(method) requires a sign-up method key");
6
- const handles = createActionHandles(() => useUserSession().signUp, "signUp");
6
+ const handles = createActionHandles(() => useAuthActionNamespaces().signUp, "signUp");
7
7
  return handles[method];
8
8
  }
@@ -1,16 +1,13 @@
1
- import type { AppAuthClient, AuthSession, AuthUser } from '#nuxt-better-auth';
2
1
  import type { ComputedRef, Ref } from 'vue';
2
+ import type { AuthSession, AuthUser } from '#nuxt-better-auth';
3
3
  export interface SignOutOptions {
4
4
  onSuccess?: () => void | Promise<void>;
5
5
  }
6
6
  export interface UseUserSessionReturn {
7
- client: AppAuthClient | null;
8
7
  session: Ref<AuthSession | null>;
9
8
  user: Ref<AuthUser | null>;
10
9
  loggedIn: ComputedRef<boolean>;
11
10
  ready: ComputedRef<boolean>;
12
- signIn: NonNullable<AppAuthClient>['signIn'];
13
- signUp: NonNullable<AppAuthClient>['signUp'];
14
11
  signOut: (options?: SignOutOptions) => Promise<void>;
15
12
  waitForSession: () => Promise<void>;
16
13
  fetchSession: (options?: {
@@ -20,3 +17,7 @@ export interface UseUserSessionReturn {
20
17
  updateUser: (updates: Partial<AuthUser>) => Promise<void>;
21
18
  }
22
19
  export declare function useUserSession(): UseUserSessionReturn;
20
+ export declare function useAuthActionNamespaces(): {
21
+ signIn: any;
22
+ signUp: any;
23
+ };
@@ -1,24 +1,27 @@
1
- import createAppAuthClient from "#auth/client";
2
1
  import { computed, navigateTo, nextTick, useNuxtApp, useRequestURL, useRuntimeConfig, useState, watch } from "#imports";
3
2
  import { normalizeAuthActionError } from "../internal/auth-action-error.js";
4
3
  import { resolvePostAuthSuccessRedirect, withFallbackSocialCallbackURL } from "../internal/redirect-helpers.js";
5
4
  import { fetchSessionClient, fetchSessionServer, stripToken } from "../internal/session-fetch.js";
6
5
  import { isRecord } from "../internal/utils.js";
6
+ import { createVueSafeAuthFacade, isAuthProxyProbeKey } from "../internal/vue-safe-auth-proxy.js";
7
7
  import { wrapAuthMethod } from "../internal/wrap-auth-method.js";
8
+ import { getAuthRuntimeFlags, useRawAuthClient } from "./useAuthClient.js";
8
9
  let _sessionSignalListenerBound = false;
9
10
  let _signOutPromise = null;
10
- let _client = null;
11
- function getClient(baseURL) {
12
- if (!_client)
13
- _client = createAppAuthClient(baseURL);
14
- return _client;
15
- }
16
- function getRuntimeFlags() {
17
- const globalFlags = globalThis.__NUXT_BETTER_AUTH_TEST_FLAGS__;
18
- if (globalFlags)
19
- return globalFlags;
20
- return { client: Boolean(import.meta.client), server: Boolean(import.meta.server) };
11
+ function createServerOnlyActionNamespace(path) {
12
+ return new Proxy({}, {
13
+ get(_target, prop) {
14
+ if (isAuthProxyProbeKey(prop))
15
+ return void 0;
16
+ const key = prop;
17
+ return async () => {
18
+ throw new Error(`${path}.${key}() can only be called on client-side`);
19
+ };
20
+ }
21
+ });
21
22
  }
23
+ const _signInServerOnly = createServerOnlyActionNamespace("signIn");
24
+ const _signUpServerOnly = createServerOnlyActionNamespace("signUp");
22
25
  function ensureSessionSignalListener(client, onSignal) {
23
26
  if (_sessionSignalListenerBound)
24
27
  return;
@@ -38,11 +41,10 @@ function ensureSessionSignalListener(client, onSignal) {
38
41
  });
39
42
  }
40
43
  export function useUserSession() {
41
- const runtimeFlags = getRuntimeFlags();
44
+ const runtimeFlags = getAuthRuntimeFlags();
42
45
  const runtimeConfig = useRuntimeConfig();
43
- const requestURL = useRequestURL();
44
46
  const nuxtApp = useNuxtApp();
45
- const client = runtimeFlags.client ? getClient(runtimeConfig.public.siteUrl || requestURL.origin) : null;
47
+ const rawClient = useRawAuthClient();
46
48
  const session = useState("auth:session", () => null);
47
49
  const user = useState("auth:user", () => null);
48
50
  const authReady = useState("auth:ready", () => false);
@@ -93,18 +95,18 @@ export function useUserSession() {
93
95
  async function fetchSession(options = {}) {
94
96
  if (runtimeFlags.server)
95
97
  return fetchSessionServer(session, user, authReady, options);
96
- if (client)
97
- return fetchSessionClient(client, session, user, authReady, options);
98
+ if (rawClient)
99
+ return fetchSessionClient(rawClient, session, user, authReady, options);
98
100
  }
99
101
  async function updateUser(updates) {
100
102
  if (!user.value)
101
103
  return;
102
104
  const previousUser = user.value;
103
105
  user.value = { ...user.value, ...updates };
104
- if (!client)
106
+ if (!rawClient)
105
107
  return;
106
108
  try {
107
- const clientWithUpdateUser = client;
109
+ const clientWithUpdateUser = rawClient;
108
110
  const result = await clientWithUpdateUser.updateUser(updates);
109
111
  if (result?.error) {
110
112
  if (result.error instanceof Error)
@@ -121,8 +123,8 @@ export function useUserSession() {
121
123
  throw error;
122
124
  }
123
125
  }
124
- if (runtimeFlags.client && client && !shouldSkipInitialClientSessionFetch.value) {
125
- const clientSession = client.useSession();
126
+ if (runtimeFlags.client && rawClient && !shouldSkipInitialClientSessionFetch.value) {
127
+ const clientSession = rawClient.useSession();
126
128
  watch(
127
129
  () => clientSession.value,
128
130
  (newSession) => {
@@ -170,59 +172,18 @@ export function useUserSession() {
170
172
  }, 5e3);
171
173
  });
172
174
  }
173
- const wrapDeps = {
174
- fetchSession,
175
- loggedIn,
176
- waitForSession,
177
- resolvePostAuthSuccessRedirect: () => resolvePostAuthSuccessRedirect(requestURL)
178
- };
179
- const signIn = client?.signIn ? new Proxy(client.signIn, {
180
- get(target, prop) {
181
- const targetRecord = target;
182
- const method = targetRecord[prop];
183
- if (typeof method !== "function")
184
- return method;
185
- const shouldSkipSessionSync = prop === "social" ? (data) => {
186
- const socialData = isRecord(data) ? data : void 0;
187
- return socialData?.disableRedirect !== true;
188
- } : void 0;
189
- const transformData = prop === "social" ? (data) => withFallbackSocialCallbackURL(data, requestURL) : void 0;
190
- return wrapAuthMethod(
191
- (...args) => targetRecord[prop](...args),
192
- wrapDeps,
193
- { shouldSkipSessionSync, transformData }
194
- );
195
- }
196
- }) : new Proxy({}, {
197
- get: (_, prop) => {
198
- throw new Error(`signIn.${String(prop)}() can only be called on client-side`);
199
- }
200
- });
201
- const signUp = client?.signUp ? new Proxy(client.signUp, {
202
- get(target, prop) {
203
- const targetRecord = target;
204
- const method = targetRecord[prop];
205
- if (typeof method !== "function")
206
- return method;
207
- return wrapAuthMethod((...args) => targetRecord[prop](...args), wrapDeps);
208
- }
209
- }) : new Proxy({}, {
210
- get: (_, prop) => {
211
- throw new Error(`signUp.${String(prop)}() can only be called on client-side`);
212
- }
213
- });
214
- if (runtimeFlags.client && client && shouldSkipInitialClientSessionFetch.value) {
215
- ensureSessionSignalListener(client, () => fetchSession({ force: true }));
175
+ if (runtimeFlags.client && rawClient && shouldSkipInitialClientSessionFetch.value) {
176
+ ensureSessionSignalListener(rawClient, () => fetchSession({ force: true }));
216
177
  }
217
178
  async function signOut(options) {
218
- if (!client)
179
+ if (!rawClient)
219
180
  throw new Error("signOut can only be called on client-side");
220
181
  if (_signOutPromise) {
221
182
  await _signOutPromise;
222
183
  return;
223
184
  }
224
185
  _signOutPromise = (async () => {
225
- await client.signOut();
186
+ await rawClient.signOut();
226
187
  clearSession();
227
188
  if (options?.onSuccess) {
228
189
  await options.onSuccess();
@@ -240,16 +201,48 @@ export function useUserSession() {
240
201
  await _signOutPromise;
241
202
  }
242
203
  return {
243
- client,
244
204
  session,
245
205
  user,
246
206
  loggedIn,
247
207
  ready,
248
- signIn,
249
- signUp,
250
208
  signOut,
251
209
  waitForSession,
252
210
  fetchSession,
253
211
  updateUser
254
212
  };
255
213
  }
214
+ export function useAuthActionNamespaces() {
215
+ const rawClient = useRawAuthClient();
216
+ const auth = useUserSession();
217
+ const requestURL = useRequestURL();
218
+ const wrapDeps = {
219
+ fetchSession: auth.fetchSession,
220
+ loggedIn: auth.loggedIn,
221
+ waitForSession: auth.waitForSession,
222
+ resolvePostAuthSuccessRedirect: () => resolvePostAuthSuccessRedirect(requestURL)
223
+ };
224
+ const signIn = rawClient?.signIn ? createVueSafeAuthFacade((prop) => {
225
+ const targetRecord = rawClient.signIn;
226
+ const method = targetRecord[prop];
227
+ if (typeof method !== "function")
228
+ return method;
229
+ const shouldSkipSessionSync = prop === "social" ? (data) => {
230
+ const socialData = isRecord(data) ? data : void 0;
231
+ return socialData?.disableRedirect !== true;
232
+ } : void 0;
233
+ const transformData = prop === "social" ? (data) => withFallbackSocialCallbackURL(data, requestURL) : void 0;
234
+ return wrapAuthMethod(
235
+ (...args) => targetRecord[prop](...args),
236
+ wrapDeps,
237
+ { shouldSkipSessionSync, transformData }
238
+ );
239
+ }) : _signInServerOnly;
240
+ const signUp = rawClient?.signUp ? createVueSafeAuthFacade((prop) => {
241
+ const targetRecord = rawClient.signUp;
242
+ const method = targetRecord[prop];
243
+ if (typeof method !== "function")
244
+ return method;
245
+ return wrapAuthMethod((...args) => targetRecord[prop](...args), wrapDeps);
246
+ }) : _signUpServerOnly;
247
+ return { signIn, signUp };
248
+ }
@@ -0,0 +1,3 @@
1
+ import type { UseUserSessionReturn } from './useUserSession.js';
2
+ export type UseUserSessionStateReturn = UseUserSessionReturn;
3
+ export declare function useUserSessionState(): UseUserSessionStateReturn;
@@ -0,0 +1,4 @@
1
+ import { useUserSession } from "./useUserSession.js";
2
+ export function useUserSessionState() {
3
+ return useUserSession();
4
+ }
@@ -1,5 +1,5 @@
1
- import type { AppAuthClient, AuthSession, AuthUser } from '#nuxt-better-auth';
2
1
  import type { Ref } from 'vue';
2
+ import type { AppAuthClient, AuthSession, AuthUser } from '#nuxt-better-auth';
3
3
  export declare function stripToken(session: AuthSession & {
4
4
  token?: string;
5
5
  }): AuthSession;
@@ -0,0 +1,3 @@
1
+ export declare function isAuthProxyProbeKey(prop: PropertyKey): boolean;
2
+ export declare function createVueSafeAuthFacade<T extends object>(resolve: (prop: PropertyKey, receiver: object) => unknown): T;
3
+ export declare function createVueSafeAuthProxy<T>(target: T): T;
@@ -0,0 +1,68 @@
1
+ const promiseProbeKeys = /* @__PURE__ */ new Set(["then", "catch", "finally"]);
2
+ export function isAuthProxyProbeKey(prop) {
3
+ return typeof prop !== "string" || promiseProbeKeys.has(prop) || prop.startsWith("__v");
4
+ }
5
+ function isObjectLike(value) {
6
+ return typeof value === "object" && value !== null || typeof value === "function";
7
+ }
8
+ export function createVueSafeAuthFacade(resolve) {
9
+ const propertyCache = /* @__PURE__ */ new Map();
10
+ const facadeTarget = {};
11
+ Object.defineProperty(facadeTarget, "__v_skip", {
12
+ value: true,
13
+ configurable: true
14
+ });
15
+ return new Proxy(facadeTarget, {
16
+ get(_target, prop, receiver) {
17
+ if (prop === "__v_skip")
18
+ return true;
19
+ if (isAuthProxyProbeKey(prop))
20
+ return void 0;
21
+ if (propertyCache.has(prop))
22
+ return propertyCache.get(prop);
23
+ const value = resolve(prop, receiver);
24
+ propertyCache.set(prop, value);
25
+ return value;
26
+ }
27
+ });
28
+ }
29
+ export function createVueSafeAuthProxy(target) {
30
+ if (!isObjectLike(target))
31
+ return target;
32
+ const cache = /* @__PURE__ */ new WeakMap();
33
+ const wrap = (value) => {
34
+ if (!isObjectLike(value))
35
+ return value;
36
+ const cached = cache.get(value);
37
+ if (cached)
38
+ return cached;
39
+ const propertyCache = /* @__PURE__ */ new Map();
40
+ const handler = {
41
+ get(target2, prop, receiver) {
42
+ if (prop === "__v_skip")
43
+ return true;
44
+ if (isAuthProxyProbeKey(prop))
45
+ return void 0;
46
+ if (propertyCache.has(prop))
47
+ return propertyCache.get(prop);
48
+ const wrapped = wrap(Reflect.get(target2, prop, receiver));
49
+ propertyCache.set(prop, wrapped);
50
+ return wrapped;
51
+ }
52
+ };
53
+ if (typeof value === "function") {
54
+ handler.apply = (target2, thisArg, args) => Reflect.apply(target2, thisArg, args);
55
+ }
56
+ const proxy = new Proxy(value, handler);
57
+ cache.set(value, proxy);
58
+ return proxy;
59
+ };
60
+ const createRootFacade = (value) => {
61
+ if (!isObjectLike(value))
62
+ return value;
63
+ const proxy = createVueSafeAuthFacade((prop, receiver) => wrap(Reflect.get(value, prop, receiver)));
64
+ cache.set(value, proxy);
65
+ return proxy;
66
+ };
67
+ return createRootFacade(target);
68
+ }
@@ -1,7 +1,8 @@
1
- import { createError, defineNuxtRouteMiddleware, getRouteRules, navigateTo, useNuxtApp, useRequestHeaders, useRuntimeConfig, useUserSession } from "#imports";
2
1
  import { defu } from "defu";
3
2
  import { createRouter, toRouteMatcher } from "radix3";
3
+ import { createError, defineNuxtRouteMiddleware, getRouteRules, navigateTo, useNuxtApp, useRequestHeaders, useRuntimeConfig } from "#imports";
4
4
  import { matchesUser } from "../../utils/match-user.js";
5
+ import { useUserSession } from "../composables/useUserSession.js";
5
6
  let authRouteRulesPromise = null;
6
7
  let routeRulesMatcherPromise = null;
7
8
  export default defineNuxtRouteMiddleware(async (to) => {
@@ -25,13 +26,13 @@ export default defineNuxtRouteMiddleware(async (to) => {
25
26
  return;
26
27
  const config = useRuntimeConfig().public.auth;
27
28
  const { fetchSession, user, loggedIn } = useUserSession();
28
- if (!loggedIn.value) {
29
+ const mode = typeof auth === "string" ? auth : auth?.only ?? "user";
30
+ const redirectTo = typeof auth === "object" ? auth.redirectTo : void 0;
31
+ if (!loggedIn.value || mode === "guest") {
29
32
  const headers = import.meta.server ? useRequestHeaders(["cookie"]) : void 0;
30
33
  const isHydratedPrerenderPayload = (import.meta.client || !import.meta.server) && nuxtApp.isHydrating && Boolean(nuxtApp.payload.prerenderedAt || nuxtApp.payload.isCached);
31
34
  await fetchSession({ headers, ...isHydratedPrerenderPayload ? { force: true } : {} });
32
35
  }
33
- const mode = typeof auth === "string" ? auth : auth?.only ?? "user";
34
- const redirectTo = typeof auth === "object" ? auth.redirectTo : void 0;
35
36
  if (mode === "guest") {
36
37
  if (loggedIn.value)
37
38
  return navigateTo(redirectTo ?? config?.redirects?.guest ?? "/");
@@ -1,4 +1,5 @@
1
- import { defineNuxtPlugin, useUserSession } from "#imports";
1
+ import { defineNuxtPlugin } from "#imports";
2
+ import { useUserSession } from "../composables/useUserSession.js";
2
3
  export default defineNuxtPlugin(async (nuxtApp) => {
3
4
  const { fetchSession } = useUserSession();
4
5
  const safeFetch = async () => {
@@ -0,0 +1,11 @@
1
+ export { useAction } from './app/composables/useAction.js';
2
+ export { useAuthAsyncData } from './app/composables/useAuthAsyncData.js';
3
+ export { useAuthClient } from './app/composables/useAuthClient.js';
4
+ export { useAuthClientAction } from './app/composables/useAuthClientAction.js';
5
+ export { useAuthRequestFetch } from './app/composables/useAuthRequestFetch.js';
6
+ export { useSignIn } from './app/composables/useSignIn.js';
7
+ export { useSignUp } from './app/composables/useSignUp.js';
8
+ export { useUserSession } from './app/composables/useUserSession.js';
9
+ export type { SignOutOptions, UseUserSessionReturn } from './app/composables/useUserSession.js';
10
+ export { useUserSessionState } from './app/composables/useUserSessionState.js';
11
+ export type { UseUserSessionStateReturn } from './app/composables/useUserSessionState.js';
@@ -0,0 +1,9 @@
1
+ export { useAction } from "./app/composables/useAction.js";
2
+ export { useAuthAsyncData } from "./app/composables/useAuthAsyncData.js";
3
+ export { useAuthClient } from "./app/composables/useAuthClient.js";
4
+ export { useAuthClientAction } from "./app/composables/useAuthClientAction.js";
5
+ export { useAuthRequestFetch } from "./app/composables/useAuthRequestFetch.js";
6
+ export { useSignIn } from "./app/composables/useSignIn.js";
7
+ export { useSignUp } from "./app/composables/useSignUp.js";
8
+ export { useUserSession } from "./app/composables/useUserSession.js";
9
+ export { useUserSessionState } from "./app/composables/useUserSessionState.js";
@@ -1,9 +1,11 @@
1
1
  import type { BetterAuthOptions, BetterAuthPlugin } from 'better-auth';
2
2
  import type { BetterAuthClientOptions } from 'better-auth/client';
3
3
  import type { Casing } from 'drizzle-orm/utils';
4
- import type { ServerAuthContext } from './types/augment.js';
4
+ import type { ServerAuthContext as BaseServerAuthContext } from './types/augment.js';
5
5
  import { createAuthClient } from 'better-auth/vue';
6
- export type { ServerAuthContext };
6
+ export interface ServerAuthContextExtension {
7
+ }
8
+ export type ServerAuthContext = BaseServerAuthContext & ServerAuthContextExtension;
7
9
  export interface ClientAuthContext {
8
10
  siteUrl: string;
9
11
  }
@@ -1,8 +1,4 @@
1
1
  import { z } from 'zod';
2
- export declare const paginationQuerySchema: z.ZodObject<{
3
- page: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
4
- limit: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
5
- search: z.ZodDefault<z.ZodString>;
6
- }, z.core.$strip>;
2
+ export declare const paginationQuerySchema: any;
7
3
  export type PaginationQuery = z.infer<typeof paginationQuerySchema>;
8
4
  export declare function sanitizeSearchPattern(search: string): string;
@@ -7,8 +7,8 @@ declare const _default: import("h3").EventHandler<import("h3").EventHandlerReque
7
7
  } | {
8
8
  accounts: any;
9
9
  total: any;
10
- page: number;
11
- limit: number;
10
+ page: any;
11
+ limit: any;
12
12
  error?: undefined;
13
13
  }>>;
14
14
  export default _default;
@@ -1,5 +1,5 @@
1
1
  import { defineEventHandler } from "h3";
2
- import { useRuntimeConfig } from "nitropack/runtime";
2
+ import { useRuntimeConfig } from "#imports";
3
3
  import { serverAuth } from "../../utils/auth.js";
4
4
  export default defineEventHandler(async (event) => {
5
5
  try {
@@ -9,8 +9,8 @@ declare const _default: import("h3").EventHandler<import("h3").EventHandlerReque
9
9
  } | {
10
10
  sessions: SafeSession[];
11
11
  total: any;
12
- page: number;
13
- limit: number;
12
+ page: any;
13
+ limit: any;
14
14
  error?: undefined;
15
15
  }>>;
16
16
  export default _default;
@@ -7,8 +7,8 @@ declare const _default: import("h3").EventHandler<import("h3").EventHandlerReque
7
7
  } | {
8
8
  users: any;
9
9
  total: any;
10
- page: number;
11
- limit: number;
10
+ page: any;
11
+ limit: any;
12
12
  error?: undefined;
13
13
  }>>;
14
14
  export default _default;
@@ -1,5 +1,5 @@
1
1
  import { createError, defineEventHandler, getRequestURL } from "h3";
2
- import { getRouteRules } from "nitropack/runtime";
2
+ import { getRouteRules } from "#imports";
3
3
  import { matchesUser } from "../../utils/match-user.js";
4
4
  import { getUserSession, requireUserSession } from "../utils/session.js";
5
5
  export default defineEventHandler(async (event) => {
@@ -1,8 +1,19 @@
1
+ import type { BetterAuthOptions } from 'better-auth';
1
2
  import type { H3Event } from 'h3';
2
- import createServerAuth from '#auth/server';
3
3
  import { betterAuth } from 'better-auth';
4
+ import createServerAuth from '#auth/server';
4
5
  type AuthOptions = ReturnType<typeof createServerAuth>;
5
- type AuthInstance = ReturnType<typeof betterAuth<AuthOptions>>;
6
+ type UserAuthConfig = AuthOptions & {
7
+ trustedOrigins?: BetterAuthOptions['trustedOrigins'];
8
+ secondaryStorage?: BetterAuthOptions['secondaryStorage'];
9
+ };
10
+ type ResolvedAuthOptions = UserAuthConfig & {
11
+ secret: string;
12
+ baseURL: string;
13
+ trustedOrigins?: BetterAuthOptions['trustedOrigins'];
14
+ database?: BetterAuthOptions['database'];
15
+ };
16
+ type AuthInstance = ReturnType<typeof betterAuth<ResolvedAuthOptions>>;
6
17
  /** Returns Better Auth instance. Caches per resolved host (or single instance when siteUrl is explicit). */
7
18
  export declare function serverAuth(event?: H3Event): AuthInstance;
8
19
  export {};