@onmax/nuxt-better-auth 0.0.2-alpha.30 → 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 (51) hide show
  1. package/README.md +60 -17
  2. package/dist/module.d.mts +0 -9
  3. package/dist/module.json +2 -2
  4. package/dist/module.mjs +668 -267
  5. package/dist/runtime/app/components/BetterAuthState.d.vue.ts +4 -4
  6. package/dist/runtime/app/components/BetterAuthState.vue +1 -1
  7. package/dist/runtime/app/components/BetterAuthState.vue.d.ts +4 -4
  8. package/dist/runtime/app/composables/useAuthClient.d.ts +9 -0
  9. package/dist/runtime/app/composables/useAuthClient.js +34 -0
  10. package/dist/runtime/app/composables/useAuthClientAction.d.ts +1 -3
  11. package/dist/runtime/app/composables/useAuthClientAction.js +2 -2
  12. package/dist/runtime/app/composables/useAuthRequestFetch.d.ts +1 -1
  13. package/dist/runtime/app/composables/useSignIn.js +2 -2
  14. package/dist/runtime/app/composables/useSignUp.js +2 -2
  15. package/dist/runtime/app/composables/useUserSession.d.ts +5 -4
  16. package/dist/runtime/app/composables/useUserSession.js +91 -218
  17. package/dist/runtime/app/composables/useUserSessionState.d.ts +3 -0
  18. package/dist/runtime/app/composables/useUserSessionState.js +4 -0
  19. package/dist/runtime/app/internal/auth-action-error.js +1 -3
  20. package/dist/runtime/app/internal/auth-action-handles.js +1 -3
  21. package/dist/runtime/app/internal/redirect-helpers.d.ts +4 -0
  22. package/dist/runtime/app/internal/redirect-helpers.js +37 -0
  23. package/dist/runtime/app/internal/session-fetch.d.ts +12 -0
  24. package/dist/runtime/app/internal/session-fetch.js +56 -0
  25. package/dist/runtime/app/internal/utils.d.ts +1 -0
  26. package/dist/runtime/app/internal/utils.js +3 -0
  27. package/dist/runtime/app/internal/vue-safe-auth-proxy.d.ts +3 -0
  28. package/dist/runtime/app/internal/vue-safe-auth-proxy.js +68 -0
  29. package/dist/runtime/app/internal/wrap-auth-method.d.ts +15 -0
  30. package/dist/runtime/app/internal/wrap-auth-method.js +66 -0
  31. package/dist/runtime/app/middleware/auth.global.js +5 -4
  32. package/dist/runtime/app/plugins/session.client.js +2 -1
  33. package/dist/runtime/composables.d.ts +11 -0
  34. package/dist/runtime/composables.js +9 -0
  35. package/dist/runtime/config.d.ts +8 -6
  36. package/dist/runtime/config.js +3 -1
  37. package/dist/runtime/server/api/_better-auth/_schema.d.ts +1 -5
  38. package/dist/runtime/server/api/_better-auth/_schema.js +2 -1
  39. package/dist/runtime/server/api/_better-auth/accounts.get.d.ts +2 -2
  40. package/dist/runtime/server/api/_better-auth/config.get.js +1 -1
  41. package/dist/runtime/server/api/_better-auth/sessions.get.d.ts +2 -2
  42. package/dist/runtime/server/api/_better-auth/users.get.d.ts +2 -2
  43. package/dist/runtime/server/middleware/route-access.js +1 -1
  44. package/dist/runtime/server/utils/auth.d.ts +13 -2
  45. package/dist/runtime/server/utils/auth.js +42 -16
  46. package/dist/runtime/server/utils/session.d.ts +3 -1
  47. package/dist/runtime/server/utils/session.js +175 -1
  48. package/dist/runtime/server/virtual-modules.d.ts +5 -0
  49. package/dist/runtime/types/augment.d.ts +1 -3
  50. package/dist/runtime/types.d.ts +1 -1
  51. package/package.json +33 -26
@@ -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,22 +1,27 @@
1
- import createAppAuthClient from "#auth/client";
2
- import { computed, navigateTo, nextTick, useNuxtApp, useRequestFetch, useRequestHeaders, useRequestURL, useRuntimeConfig, useState, watch } from "#imports";
1
+ import { computed, navigateTo, nextTick, useNuxtApp, useRequestURL, useRuntimeConfig, useState, watch } from "#imports";
3
2
  import { normalizeAuthActionError } from "../internal/auth-action-error.js";
3
+ import { resolvePostAuthSuccessRedirect, withFallbackSocialCallbackURL } from "../internal/redirect-helpers.js";
4
+ import { fetchSessionClient, fetchSessionServer, stripToken } from "../internal/session-fetch.js";
5
+ import { isRecord } from "../internal/utils.js";
6
+ import { createVueSafeAuthFacade, isAuthProxyProbeKey } from "../internal/vue-safe-auth-proxy.js";
7
+ import { wrapAuthMethod } from "../internal/wrap-auth-method.js";
8
+ import { getAuthRuntimeFlags, useRawAuthClient } from "./useAuthClient.js";
4
9
  let _sessionSignalListenerBound = false;
5
- let _client = null;
6
- function isRecord(value) {
7
- return Boolean(value && typeof value === "object");
8
- }
9
- function getClient(baseURL) {
10
- if (!_client)
11
- _client = createAppAuthClient(baseURL);
12
- return _client;
13
- }
14
- function getRuntimeFlags() {
15
- const globalFlags = globalThis.__NUXT_BETTER_AUTH_TEST_FLAGS__;
16
- if (globalFlags)
17
- return globalFlags;
18
- return { client: Boolean(import.meta.client), server: Boolean(import.meta.server) };
10
+ let _signOutPromise = null;
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
+ });
19
22
  }
23
+ const _signInServerOnly = createServerOnlyActionNamespace("signIn");
24
+ const _signUpServerOnly = createServerOnlyActionNamespace("signUp");
20
25
  function ensureSessionSignalListener(client, onSignal) {
21
26
  if (_sessionSignalListenerBound)
22
27
  return;
@@ -36,11 +41,10 @@ function ensureSessionSignalListener(client, onSignal) {
36
41
  });
37
42
  }
38
43
  export function useUserSession() {
39
- const runtimeFlags = getRuntimeFlags();
44
+ const runtimeFlags = getAuthRuntimeFlags();
40
45
  const runtimeConfig = useRuntimeConfig();
41
- const requestURL = useRequestURL();
42
46
  const nuxtApp = useNuxtApp();
43
- const client = runtimeFlags.client ? getClient(runtimeConfig.public.siteUrl || requestURL.origin) : null;
47
+ const rawClient = useRawAuthClient();
44
48
  const session = useState("auth:session", () => null);
45
49
  const user = useState("auth:user", () => null);
46
50
  const authReady = useState("auth:ready", () => false);
@@ -88,15 +92,21 @@ export function useUserSession() {
88
92
  session.value = null;
89
93
  user.value = null;
90
94
  }
95
+ async function fetchSession(options = {}) {
96
+ if (runtimeFlags.server)
97
+ return fetchSessionServer(session, user, authReady, options);
98
+ if (rawClient)
99
+ return fetchSessionClient(rawClient, session, user, authReady, options);
100
+ }
91
101
  async function updateUser(updates) {
92
102
  if (!user.value)
93
103
  return;
94
104
  const previousUser = user.value;
95
105
  user.value = { ...user.value, ...updates };
96
- if (!client)
106
+ if (!rawClient)
97
107
  return;
98
108
  try {
99
- const clientWithUpdateUser = client;
109
+ const clientWithUpdateUser = rawClient;
100
110
  const result = await clientWithUpdateUser.updateUser(updates);
101
111
  if (result?.error) {
102
112
  if (result.error instanceof Error)
@@ -113,8 +123,8 @@ export function useUserSession() {
113
123
  throw error;
114
124
  }
115
125
  }
116
- if (runtimeFlags.client && client && !shouldSkipInitialClientSessionFetch.value) {
117
- const clientSession = client.useSession();
126
+ if (runtimeFlags.client && rawClient && !shouldSkipInitialClientSessionFetch.value) {
127
+ const clientSession = rawClient.useSession();
118
128
  watch(
119
129
  () => clientSession.value,
120
130
  (newSession) => {
@@ -122,8 +132,7 @@ export function useUserSession() {
122
132
  if (shouldWaitForPrerenderResolution)
123
133
  return;
124
134
  if (newSession?.data?.session && newSession?.data?.user) {
125
- const { token: _, ...safeSession } = newSession.data.session;
126
- session.value = safeSession;
135
+ session.value = stripToken(newSession.data.session);
127
136
  user.value = newSession.data.user;
128
137
  } else if (!newSession?.isPending && !newSession?.isRefetching) {
129
138
  const isHydrationEmptySnapshot = nuxtApp.isHydrating && nuxtApp.payload.serverRendered && Boolean(session.value && user.value) && !newSession?.data?.session && !newSession?.data?.user;
@@ -163,213 +172,77 @@ export function useUserSession() {
163
172
  }, 5e3);
164
173
  });
165
174
  }
166
- function isSafeLocalRedirect(redirect) {
167
- if (typeof redirect !== "string")
168
- return;
169
- if (!redirect.startsWith("/") || redirect.startsWith("//"))
170
- return;
171
- return redirect;
172
- }
173
- function resolvePostAuthRedirect() {
174
- const authConfig = runtimeConfig.public.auth;
175
- const redirectQueryKey = authConfig?.redirectQueryKey ?? "redirect";
176
- const queryRedirect = requestURL.searchParams?.get(redirectQueryKey);
177
- const safeQueryRedirect = isSafeLocalRedirect(queryRedirect);
178
- if (safeQueryRedirect)
179
- return safeQueryRedirect;
180
- return isSafeLocalRedirect(authConfig?.redirects?.authenticated);
181
- }
182
- function resolvePostAuthSuccessRedirect() {
183
- const target = resolvePostAuthRedirect();
184
- if (!target)
185
- return;
186
- return async () => {
187
- await navigateTo(target);
188
- };
189
- }
190
- function withFallbackSocialCallbackURL(data) {
191
- const callbackURL = resolvePostAuthRedirect();
192
- if (!callbackURL)
193
- return data;
194
- if (!isRecord(data))
195
- return { callbackURL };
196
- if (typeof data.callbackURL === "string")
197
- return data;
198
- return {
199
- ...data,
200
- callbackURL
201
- };
202
- }
203
- function wrapOnSuccess(cb) {
204
- return async (ctx) => {
205
- await fetchSession({ force: true });
206
- if (!loggedIn.value)
207
- await waitForSession();
208
- await nextTick();
209
- await cb(ctx);
210
- };
211
- }
212
- function wrapAuthMethod(method, wrapOptions = {}) {
213
- return (async (...args) => {
214
- const originalData = args[0];
215
- const options = args[1];
216
- const data = wrapOptions.transformData?.(originalData, options) ?? originalData;
217
- const dataRecord = isRecord(data) ? data : void 0;
218
- const optionsRecord = isRecord(options) ? options : void 0;
219
- if (wrapOptions.shouldSkipSessionSync?.(data, options))
220
- return method(data, options);
221
- const fetchOptions = isRecord(dataRecord?.fetchOptions) ? dataRecord.fetchOptions : void 0;
222
- const nestedOnSuccess = fetchOptions?.onSuccess;
223
- const topLevelOnSuccess = optionsRecord?.onSuccess;
224
- const fallbackOnSuccess = resolvePostAuthSuccessRedirect();
225
- const wrappedFallbackOnSuccess = fallbackOnSuccess && wrapOnSuccess(async () => {
226
- if (!loggedIn.value)
227
- return;
228
- await fallbackOnSuccess();
229
- });
230
- if (typeof nestedOnSuccess === "function") {
231
- const nextData = {
232
- ...dataRecord,
233
- fetchOptions: {
234
- ...fetchOptions,
235
- onSuccess: wrapOnSuccess(nestedOnSuccess)
236
- }
237
- };
238
- return method(nextData, options);
239
- }
240
- if (typeof topLevelOnSuccess === "function") {
241
- const nextOptions = {
242
- ...optionsRecord,
243
- onSuccess: wrapOnSuccess(topLevelOnSuccess)
244
- };
245
- return method(data, nextOptions);
246
- }
247
- if (wrappedFallbackOnSuccess) {
248
- if (fetchOptions) {
249
- const nextData = {
250
- ...dataRecord,
251
- fetchOptions: {
252
- ...fetchOptions,
253
- onSuccess: wrappedFallbackOnSuccess
254
- }
255
- };
256
- return method(nextData, options);
257
- }
258
- const nextOptions = {
259
- ...optionsRecord,
260
- onSuccess: wrappedFallbackOnSuccess
261
- };
262
- return method(data, nextOptions);
263
- }
264
- return method(data, options);
265
- });
266
- }
267
- const signIn = client?.signIn ? new Proxy(client.signIn, {
268
- get(target, prop) {
269
- const targetRecord = target;
270
- const method = targetRecord[prop];
271
- if (typeof method !== "function")
272
- return method;
273
- const shouldSkipSessionSync = prop === "social" ? (data) => {
274
- const socialData = isRecord(data) ? data : void 0;
275
- return socialData?.disableRedirect !== true;
276
- } : void 0;
277
- const transformData = prop === "social" ? withFallbackSocialCallbackURL : void 0;
278
- return wrapAuthMethod(
279
- (...args) => targetRecord[prop](...args),
280
- { shouldSkipSessionSync, transformData }
281
- );
282
- }
283
- }) : new Proxy({}, {
284
- get: (_, prop) => {
285
- throw new Error(`signIn.${String(prop)}() can only be called on client-side`);
286
- }
287
- });
288
- const signUp = client?.signUp ? new Proxy(client.signUp, {
289
- get(target, prop) {
290
- const targetRecord = target;
291
- const method = targetRecord[prop];
292
- if (typeof method !== "function")
293
- return method;
294
- return wrapAuthMethod((...args) => targetRecord[prop](...args));
295
- }
296
- }) : new Proxy({}, {
297
- get: (_, prop) => {
298
- throw new Error(`signUp.${String(prop)}() can only be called on client-side`);
299
- }
300
- });
301
- async function fetchSession(options = {}) {
302
- if (runtimeFlags.server) {
303
- try {
304
- const headers = options.headers || useRequestHeaders(["cookie"]);
305
- const requestFetch = useRequestFetch();
306
- const data = await requestFetch("/api/auth/get-session", { headers });
307
- if (data?.session && data?.user) {
308
- const { token: _, ...safeSession } = data.session;
309
- session.value = safeSession;
310
- user.value = data.user;
311
- } else {
312
- clearSession();
313
- }
314
- } catch {
315
- clearSession();
316
- } finally {
317
- if (!authReady.value)
318
- authReady.value = true;
319
- }
320
- return;
321
- }
322
- if (client) {
323
- try {
324
- const headers = options.headers || useRequestHeaders(["cookie"]);
325
- const fetchOptions = headers ? { headers } : void 0;
326
- const query = options.force ? { disableCookieCache: true } : void 0;
327
- const result = await client.getSession({ query }, fetchOptions);
328
- const data = result.data;
329
- if (data?.session && data?.user) {
330
- const { token: _, ...safeSession } = data.session;
331
- session.value = safeSession;
332
- user.value = data.user;
333
- } else {
334
- clearSession();
335
- }
336
- } catch (error) {
337
- clearSession();
338
- console.error("[nuxt-better-auth] Failed to fetch session:", error);
339
- } finally {
340
- if (!authReady.value)
341
- authReady.value = true;
342
- }
343
- }
344
- }
345
- if (runtimeFlags.client && client && shouldSkipInitialClientSessionFetch.value) {
346
- ensureSessionSignalListener(client, () => fetchSession({ force: true }));
175
+ if (runtimeFlags.client && rawClient && shouldSkipInitialClientSessionFetch.value) {
176
+ ensureSessionSignalListener(rawClient, () => fetchSession({ force: true }));
347
177
  }
348
178
  async function signOut(options) {
349
- if (!client)
179
+ if (!rawClient)
350
180
  throw new Error("signOut can only be called on client-side");
351
- await client.signOut();
352
- clearSession();
353
- if (options?.onSuccess) {
354
- await options.onSuccess();
181
+ if (_signOutPromise) {
182
+ await _signOutPromise;
355
183
  return;
356
184
  }
357
- const authConfig = runtimeConfig.public.auth;
358
- const logoutRedirect = authConfig?.redirects?.logout;
359
- if (logoutRedirect)
360
- await navigateTo(logoutRedirect);
185
+ _signOutPromise = (async () => {
186
+ await rawClient.signOut();
187
+ clearSession();
188
+ if (options?.onSuccess) {
189
+ await options.onSuccess();
190
+ return;
191
+ }
192
+ const authConfig = runtimeConfig.public.auth;
193
+ const logoutRedirect = authConfig?.redirects?.logout;
194
+ if (logoutRedirect) {
195
+ await nextTick();
196
+ await navigateTo(logoutRedirect);
197
+ }
198
+ })().finally(() => {
199
+ _signOutPromise = null;
200
+ });
201
+ await _signOutPromise;
361
202
  }
362
203
  return {
363
- client,
364
204
  session,
365
205
  user,
366
206
  loggedIn,
367
207
  ready,
368
- signIn,
369
- signUp,
370
208
  signOut,
371
209
  waitForSession,
372
210
  fetchSession,
373
211
  updateUser
374
212
  };
375
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,7 +1,5 @@
1
+ import { isRecord } from "./utils.js";
1
2
  export const DEFAULT_AUTH_ACTION_ERROR_MESSAGE = "Request failed. Please try again.";
2
- function isRecord(value) {
3
- return Boolean(value && typeof value === "object");
4
- }
5
3
  function getMessage(value) {
6
4
  if (value instanceof Error)
7
5
  return value.message;
@@ -1,8 +1,6 @@
1
1
  import { ref } from "#imports";
2
2
  import { normalizeAuthActionError } from "./auth-action-error.js";
3
- function isRecord(value) {
4
- return Boolean(value && typeof value === "object");
5
- }
3
+ import { isRecord } from "./utils.js";
6
4
  function isErrorResult(value) {
7
5
  if (!isRecord(value))
8
6
  return false;
@@ -0,0 +1,4 @@
1
+ export declare function isSafeLocalRedirect(redirect: unknown): string | undefined;
2
+ export declare function resolvePostAuthRedirect(requestURL: URL): string | undefined;
3
+ export declare function resolvePostAuthSuccessRedirect(requestURL: URL): (() => Promise<void>) | undefined;
4
+ export declare function withFallbackSocialCallbackURL(data: unknown, requestURL: URL): unknown;
@@ -0,0 +1,37 @@
1
+ import { navigateTo, useRuntimeConfig } from "#imports";
2
+ import { isRecord } from "./utils.js";
3
+ export function isSafeLocalRedirect(redirect) {
4
+ if (typeof redirect !== "string")
5
+ return;
6
+ if (!redirect.startsWith("/") || redirect.startsWith("//"))
7
+ return;
8
+ return redirect;
9
+ }
10
+ export function resolvePostAuthRedirect(requestURL) {
11
+ const runtimeConfig = useRuntimeConfig();
12
+ const authConfig = runtimeConfig.public.auth;
13
+ const redirectQueryKey = authConfig?.redirectQueryKey ?? "redirect";
14
+ const queryRedirect = requestURL.searchParams?.get(redirectQueryKey);
15
+ const safeQueryRedirect = isSafeLocalRedirect(queryRedirect);
16
+ if (safeQueryRedirect)
17
+ return safeQueryRedirect;
18
+ return isSafeLocalRedirect(authConfig?.redirects?.authenticated);
19
+ }
20
+ export function resolvePostAuthSuccessRedirect(requestURL) {
21
+ const target = resolvePostAuthRedirect(requestURL);
22
+ if (!target)
23
+ return;
24
+ return async () => {
25
+ await navigateTo(target);
26
+ };
27
+ }
28
+ export function withFallbackSocialCallbackURL(data, requestURL) {
29
+ const callbackURL = resolvePostAuthRedirect(requestURL);
30
+ if (!callbackURL)
31
+ return data;
32
+ if (!isRecord(data))
33
+ return { callbackURL };
34
+ if (typeof data.callbackURL === "string")
35
+ return data;
36
+ return { ...data, callbackURL };
37
+ }
@@ -0,0 +1,12 @@
1
+ import type { Ref } from 'vue';
2
+ import type { AppAuthClient, AuthSession, AuthUser } from '#nuxt-better-auth';
3
+ export declare function stripToken(session: AuthSession & {
4
+ token?: string;
5
+ }): AuthSession;
6
+ export declare function fetchSessionServer(session: Ref<AuthSession | null>, user: Ref<AuthUser | null>, authReady: Ref<boolean>, options?: {
7
+ headers?: HeadersInit;
8
+ }): Promise<void>;
9
+ export declare function fetchSessionClient(client: AppAuthClient, session: Ref<AuthSession | null>, user: Ref<AuthUser | null>, authReady: Ref<boolean>, options?: {
10
+ headers?: HeadersInit;
11
+ force?: boolean;
12
+ }): Promise<void>;