@better-auth-ui/react 1.6.2 → 1.6.3

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 (194) hide show
  1. package/dist/hooks/auth/use-authenticate.d.ts +30 -6
  2. package/dist/hooks/auth/use-is-username-available.d.ts +12 -11
  3. package/dist/hooks/auth/use-is-username-available.js +8 -7
  4. package/dist/hooks/auth/use-request-password-reset.d.ts +11 -15
  5. package/dist/hooks/auth/use-request-password-reset.js +8 -7
  6. package/dist/hooks/auth/use-reset-password.d.ts +13 -29
  7. package/dist/hooks/auth/use-reset-password.js +8 -7
  8. package/dist/hooks/auth/use-send-verification-email.d.ts +10 -14
  9. package/dist/hooks/auth/use-send-verification-email.js +8 -7
  10. package/dist/hooks/auth/use-session.d.ts +30 -4
  11. package/dist/hooks/auth/use-session.js +11 -10
  12. package/dist/hooks/auth/use-sign-in-email.d.ts +11 -37
  13. package/dist/hooks/auth/use-sign-in-email.js +11 -12
  14. package/dist/hooks/auth/use-sign-in-magic-link.d.ts +14 -18
  15. package/dist/hooks/auth/use-sign-in-magic-link.js +8 -7
  16. package/dist/hooks/auth/use-sign-in-passkey.d.ts +10 -47
  17. package/dist/hooks/auth/use-sign-in-passkey.js +11 -12
  18. package/dist/hooks/auth/use-sign-in-social.d.ts +30 -59
  19. package/dist/hooks/auth/use-sign-in-social.js +8 -7
  20. package/dist/hooks/auth/use-sign-in-username.d.ts +12 -40
  21. package/dist/hooks/auth/use-sign-in-username.js +11 -12
  22. package/dist/hooks/auth/use-sign-out.d.ts +10 -10
  23. package/dist/hooks/auth/use-sign-out.js +10 -12
  24. package/dist/hooks/auth/use-sign-up-email.d.ts +18 -101
  25. package/dist/hooks/auth/use-sign-up-email.js +11 -12
  26. package/dist/hooks/auth/use-user.d.ts +7 -5
  27. package/dist/hooks/settings/use-account-info.d.ts +9 -9
  28. package/dist/hooks/settings/use-account-info.js +13 -16
  29. package/dist/hooks/settings/use-add-passkey.d.ts +8 -30
  30. package/dist/hooks/settings/use-add-passkey.js +10 -11
  31. package/dist/hooks/settings/use-change-email.d.ts +11 -14
  32. package/dist/hooks/settings/use-change-email.js +11 -12
  33. package/dist/hooks/settings/use-change-password.d.ts +11 -43
  34. package/dist/hooks/settings/use-change-password.js +8 -7
  35. package/dist/hooks/settings/use-delete-passkey.d.ts +10 -12
  36. package/dist/hooks/settings/use-delete-passkey.js +11 -12
  37. package/dist/hooks/settings/use-delete-user.d.ts +13 -30
  38. package/dist/hooks/settings/use-delete-user.js +8 -7
  39. package/dist/hooks/settings/use-link-social.d.ts +22 -25
  40. package/dist/hooks/settings/use-link-social.js +8 -7
  41. package/dist/hooks/settings/use-list-accounts.d.ts +8 -6
  42. package/dist/hooks/settings/use-list-accounts.js +10 -16
  43. package/dist/hooks/settings/use-list-device-sessions.d.ts +29 -6
  44. package/dist/hooks/settings/use-list-device-sessions.js +10 -16
  45. package/dist/hooks/settings/use-list-sessions.d.ts +8 -5
  46. package/dist/hooks/settings/use-list-sessions.js +10 -16
  47. package/dist/hooks/settings/use-list-user-passkeys.d.ts +8 -5
  48. package/dist/hooks/settings/use-list-user-passkeys.js +10 -16
  49. package/dist/hooks/settings/use-revoke-multi-session.d.ts +11 -11
  50. package/dist/hooks/settings/use-revoke-multi-session.js +11 -12
  51. package/dist/hooks/settings/use-revoke-session.d.ts +11 -11
  52. package/dist/hooks/settings/use-revoke-session.js +11 -12
  53. package/dist/hooks/settings/use-set-active-session.d.ts +12 -29
  54. package/dist/hooks/settings/use-set-active-session.js +12 -13
  55. package/dist/hooks/settings/use-unlink-account.d.ts +12 -12
  56. package/dist/hooks/settings/use-unlink-account.js +11 -12
  57. package/dist/hooks/settings/use-update-user.d.ts +1178 -4
  58. package/dist/hooks/settings/use-update-user.js +17 -18
  59. package/dist/hooks/use-auth-mutation.d.ts +12 -0
  60. package/dist/hooks/use-auth-mutation.js +11 -0
  61. package/dist/hooks/use-auth-query.d.ts +14 -0
  62. package/dist/hooks/use-auth-query.js +15 -0
  63. package/dist/index.d.ts +2 -2
  64. package/dist/index.js +33 -33
  65. package/dist/mutations/auth/is-username-available-options.d.ts +19 -0
  66. package/dist/mutations/auth/is-username-available-options.js +7 -0
  67. package/dist/mutations/auth/request-password-reset-options.d.ts +15 -0
  68. package/dist/mutations/auth/request-password-reset-options.js +7 -0
  69. package/dist/mutations/auth/reset-password-options.d.ts +17 -0
  70. package/dist/mutations/auth/reset-password-options.js +7 -0
  71. package/dist/mutations/auth/send-verification-email-options.d.ts +15 -0
  72. package/dist/mutations/auth/send-verification-email-options.js +7 -0
  73. package/dist/mutations/auth/sign-in-email-options.d.ts +23 -0
  74. package/dist/mutations/auth/sign-in-email-options.js +11 -0
  75. package/dist/mutations/auth/sign-in-magic-link-options.d.ts +23 -0
  76. package/dist/mutations/auth/sign-in-magic-link-options.js +11 -0
  77. package/dist/mutations/auth/sign-in-passkey-options.d.ts +15 -0
  78. package/dist/mutations/auth/sign-in-passkey-options.js +11 -0
  79. package/dist/mutations/auth/sign-in-social-options.d.ts +61 -0
  80. package/dist/mutations/auth/sign-in-social-options.js +11 -0
  81. package/dist/mutations/auth/sign-in-username-options.d.ts +19 -0
  82. package/dist/mutations/auth/sign-in-username-options.js +11 -0
  83. package/dist/mutations/auth/sign-out-options.d.ts +12 -0
  84. package/dist/mutations/auth/sign-out-options.js +7 -0
  85. package/dist/mutations/auth/sign-up-email-options.d.ts +1180 -0
  86. package/dist/mutations/auth/sign-up-email-options.js +11 -0
  87. package/dist/mutations/auth-mutation-options.d.ts +25 -0
  88. package/dist/mutations/auth-mutation-options.js +19 -0
  89. package/dist/mutations/settings/add-passkey-options.d.ts +15 -0
  90. package/dist/mutations/settings/add-passkey-options.js +11 -0
  91. package/dist/mutations/settings/change-email-options.d.ts +15 -0
  92. package/dist/mutations/settings/change-email-options.js +7 -0
  93. package/dist/mutations/settings/change-password-options.d.ts +17 -0
  94. package/dist/mutations/settings/change-password-options.js +7 -0
  95. package/dist/mutations/settings/delete-passkey-options.d.ts +13 -0
  96. package/dist/mutations/settings/delete-passkey-options.js +11 -0
  97. package/dist/mutations/settings/delete-user-options.d.ts +20 -0
  98. package/dist/mutations/settings/delete-user-options.js +7 -0
  99. package/dist/mutations/settings/link-social-options.d.ts +39 -0
  100. package/dist/mutations/settings/link-social-options.js +7 -0
  101. package/dist/mutations/settings/revoke-multi-session-options.d.ts +13 -0
  102. package/dist/mutations/settings/revoke-multi-session-options.js +11 -0
  103. package/dist/mutations/settings/revoke-session-options.d.ts +13 -0
  104. package/dist/mutations/settings/revoke-session-options.js +7 -0
  105. package/dist/mutations/settings/set-active-session-options.d.ts +13 -0
  106. package/dist/mutations/settings/set-active-session-options.js +11 -0
  107. package/dist/mutations/settings/unlink-account-options.d.ts +15 -0
  108. package/dist/mutations/settings/unlink-account-options.js +7 -0
  109. package/dist/mutations/settings/update-user-options.d.ts +1182 -0
  110. package/dist/mutations/settings/update-user-options.js +7 -0
  111. package/dist/queries/auth/session-options.d.ts +110 -0
  112. package/dist/queries/auth/session-options.js +7 -0
  113. package/dist/queries/auth-query-options.d.ts +29 -0
  114. package/dist/queries/auth-query-options.js +17 -0
  115. package/dist/queries/settings/account-info-options.d.ts +32 -0
  116. package/dist/queries/settings/account-info-options.js +12 -0
  117. package/dist/queries/settings/list-accounts-options.d.ts +53 -0
  118. package/dist/queries/settings/list-accounts-options.js +12 -0
  119. package/dist/queries/settings/list-device-sessions-options.d.ts +99 -0
  120. package/dist/queries/settings/list-device-sessions-options.js +12 -0
  121. package/dist/queries/settings/list-sessions-options.d.ts +54 -0
  122. package/dist/queries/settings/list-sessions-options.js +12 -0
  123. package/dist/queries/settings/list-user-passkeys-options.d.ts +18 -0
  124. package/dist/queries/settings/list-user-passkeys-options.js +12 -0
  125. package/package.json +1 -1
  126. package/src/hooks/auth/use-authenticate.ts +7 -10
  127. package/src/hooks/auth/use-is-username-available.ts +17 -8
  128. package/src/hooks/auth/use-request-password-reset.ts +19 -8
  129. package/src/hooks/auth/use-reset-password.ts +19 -9
  130. package/src/hooks/auth/use-send-verification-email.ts +18 -8
  131. package/src/hooks/auth/use-session.ts +20 -16
  132. package/src/hooks/auth/use-sign-in-email.ts +23 -16
  133. package/src/hooks/auth/use-sign-in-magic-link.ts +18 -10
  134. package/src/hooks/auth/use-sign-in-passkey.ts +24 -18
  135. package/src/hooks/auth/use-sign-in-social.ts +18 -8
  136. package/src/hooks/auth/use-sign-in-username.ts +24 -18
  137. package/src/hooks/auth/use-sign-out.ts +18 -17
  138. package/src/hooks/auth/use-sign-up-email.ts +24 -18
  139. package/src/hooks/auth/use-user.ts +7 -8
  140. package/src/hooks/settings/use-account-info.ts +29 -20
  141. package/src/hooks/settings/use-add-passkey.ts +22 -17
  142. package/src/hooks/settings/use-change-email.ts +21 -17
  143. package/src/hooks/settings/use-change-password.ts +18 -8
  144. package/src/hooks/settings/use-delete-passkey.ts +21 -16
  145. package/src/hooks/settings/use-delete-user.ts +18 -8
  146. package/src/hooks/settings/use-link-social.ts +18 -8
  147. package/src/hooks/settings/use-list-accounts.ts +25 -15
  148. package/src/hooks/settings/use-list-device-sessions.ts +26 -21
  149. package/src/hooks/settings/use-list-sessions.ts +25 -14
  150. package/src/hooks/settings/use-list-user-passkeys.ts +25 -16
  151. package/src/hooks/settings/use-revoke-multi-session.ts +25 -16
  152. package/src/hooks/settings/use-revoke-session.ts +25 -16
  153. package/src/hooks/settings/use-set-active-session.ts +35 -24
  154. package/src/hooks/settings/use-unlink-account.ts +22 -15
  155. package/src/hooks/settings/use-update-user.ts +29 -22
  156. package/src/hooks/use-auth-mutation.ts +29 -0
  157. package/src/hooks/use-auth-query.ts +36 -0
  158. package/src/index.ts +2 -2
  159. package/src/mutations/auth/is-username-available-options.ts +18 -0
  160. package/src/mutations/auth/request-password-reset-options.ts +14 -0
  161. package/src/mutations/auth/reset-password-options.ts +14 -0
  162. package/src/mutations/auth/send-verification-email-options.ts +14 -0
  163. package/src/mutations/auth/sign-in-email-options.ts +19 -0
  164. package/src/mutations/auth/sign-in-magic-link-options.ts +15 -0
  165. package/src/mutations/auth/sign-in-passkey-options.ts +15 -0
  166. package/src/mutations/auth/sign-in-social-options.ts +19 -0
  167. package/src/mutations/auth/sign-in-username-options.ts +15 -0
  168. package/src/mutations/auth/sign-out-options.ts +11 -0
  169. package/src/mutations/auth/sign-up-email-options.ts +15 -0
  170. package/src/mutations/auth-mutation-options.ts +49 -0
  171. package/src/mutations/settings/add-passkey-options.ts +15 -0
  172. package/src/mutations/settings/change-email-options.ts +11 -0
  173. package/src/mutations/settings/change-password-options.ts +14 -0
  174. package/src/mutations/settings/delete-passkey-options.ts +15 -0
  175. package/src/mutations/settings/delete-user-options.ts +11 -0
  176. package/src/mutations/settings/link-social-options.ts +11 -0
  177. package/src/mutations/settings/revoke-multi-session-options.ts +15 -0
  178. package/src/mutations/settings/revoke-session-options.ts +14 -0
  179. package/src/mutations/settings/set-active-session-options.ts +15 -0
  180. package/src/mutations/settings/unlink-account-options.ts +14 -0
  181. package/src/mutations/settings/update-user-options.ts +15 -0
  182. package/src/queries/auth/session-options.ts +18 -0
  183. package/src/queries/auth-query-options.ts +34 -0
  184. package/src/queries/settings/account-info-options.ts +25 -0
  185. package/src/queries/settings/list-accounts-options.ts +26 -0
  186. package/src/queries/settings/list-device-sessions-options.ts +24 -0
  187. package/src/queries/settings/list-sessions-options.ts +23 -0
  188. package/src/queries/settings/list-user-passkeys-options.ts +23 -0
  189. package/dist/hooks/auth/use-auth-mutation.d.ts +0 -18
  190. package/dist/hooks/auth/use-auth-mutation.js +0 -13
  191. package/dist/hooks/auth/use-auth-query.d.ts +0 -24
  192. package/dist/hooks/auth/use-auth-query.js +0 -13
  193. package/src/hooks/auth/use-auth-mutation.ts +0 -56
  194. package/src/hooks/auth/use-auth-query.ts +0 -50
@@ -1,43 +1,50 @@
1
- import { useQueryClient } from "@tanstack/react-query"
1
+ import { useMutation, useQueryClient } from "@tanstack/react-query"
2
2
 
3
3
  import { useAuth } from "../../components/auth/auth-provider"
4
4
  import type { AuthClient } from "../../lib/auth-client"
5
- import {
6
- type UseAuthMutationOptions,
7
- type UseAuthMutationResult,
8
- useAuthMutation
9
- } from "../auth/use-auth-mutation"
5
+ import { updateUserOptions } from "../../mutations/settings/update-user-options"
6
+ import { sessionOptions } from "../../queries/auth/session-options"
10
7
  import { useSession } from "../auth/use-session"
11
8
 
9
+ export type UseUpdateUserParams = NonNullable<
10
+ Parameters<AuthClient["updateUser"]>[0]
11
+ >
12
+
13
+ export type UseUpdateUserOptions = Omit<
14
+ ReturnType<typeof updateUserOptions>,
15
+ "mutationKey" | "mutationFn"
16
+ >
17
+
12
18
  /**
13
19
  * Hook that creates a mutation for updating the authenticated user's profile.
14
20
  *
15
- * The mutation submits the name update, refetches the session on success,
16
- * and displays success or error toasts.
21
+ * Optimistically merges the update into the cached session, then refetches
22
+ * the session to reconcile with the server.
17
23
  *
24
+ * @param options - React Query options forwarded to `useMutation`.
18
25
  * @returns The `useMutation` result.
19
26
  */
20
- export function useUpdateUser(
21
- options?: UseAuthMutationOptions<AuthClient["updateUser"]>
22
- ): UseAuthMutationResult<AuthClient["updateUser"]> {
27
+ export function useUpdateUser(options?: UseUpdateUserOptions) {
23
28
  const { authClient } = useAuth()
24
- const { data: session, refetch } = useSession({ refetchOnMount: false })
29
+ const { data: session, refetch: refetchSession } = useSession({
30
+ refetchOnMount: false
31
+ })
25
32
  const queryClient = useQueryClient()
26
33
 
27
- return useAuthMutation({
28
- authFn: authClient.updateUser,
29
- options: {
30
- ...options,
31
- onSuccess: async (data, variables, ...rest) => {
32
- queryClient.setQueryData(["auth", "getSession"], {
34
+ return useMutation({
35
+ ...updateUserOptions(authClient),
36
+ ...options,
37
+ onSuccess: async (data, variables, ...rest) => {
38
+ if (session) {
39
+ queryClient.setQueryData(sessionOptions(authClient).queryKey, {
33
40
  ...session,
34
- user: { ...session?.user, ...variables }
41
+ user: { ...session.user, ...variables }
35
42
  })
43
+ }
36
44
 
37
- refetch()
45
+ refetchSession()
38
46
 
39
- await options?.onSuccess?.(data, variables, ...rest)
40
- }
47
+ await options?.onSuccess?.(data, variables, ...rest)
41
48
  }
42
49
  })
43
50
  }
@@ -0,0 +1,29 @@
1
+ import { type MutationKey, useMutation } from "@tanstack/react-query"
2
+ import { authMutationOptions } from "../mutations/auth-mutation-options"
3
+
4
+ /**
5
+ * Escape-hatch hook for Better Auth endpoints that don't have a purpose-built
6
+ * mutation hook in this library yet. Thin wrapper over `useMutation` and
7
+ * `authMutationOptions`.
8
+ *
9
+ * @param authFn - Better Auth client method (e.g. `authClient.emailOtp.sendVerificationOtp`).
10
+ * @param mutationKey - Scope for the mutation cache key.
11
+ * @param options - React Query options forwarded to `useMutation`.
12
+ */
13
+ export function useAuthMutation<
14
+ // biome-ignore lint/suspicious/noExplicitAny: matches authMutationOptions' widened constraint
15
+ TFn extends (...args: any) => any,
16
+ const TMutationKey extends MutationKey
17
+ >(
18
+ authFn: TFn,
19
+ mutationKey: TMutationKey,
20
+ options?: Omit<
21
+ ReturnType<typeof authMutationOptions<TFn, TMutationKey>>,
22
+ "mutationKey" | "mutationFn"
23
+ >
24
+ ) {
25
+ return useMutation({
26
+ ...authMutationOptions(authFn, mutationKey),
27
+ ...options
28
+ })
29
+ }
@@ -0,0 +1,36 @@
1
+ import { type QueryKey, useQuery } from "@tanstack/react-query"
2
+ import { type AuthFn, authQueryOptions } from "../queries/auth-query-options"
3
+
4
+ type UseAuthQueryOptions<TFn extends AuthFn, TQueryKey extends QueryKey> = Omit<
5
+ ReturnType<typeof authQueryOptions<TFn, TQueryKey>>,
6
+ "queryKey" | "queryFn"
7
+ > &
8
+ NonNullable<Parameters<TFn>[0]>
9
+
10
+ /**
11
+ * Escape-hatch hook for Better Auth endpoints that don't have a purpose-built
12
+ * hook in this library yet. Thin wrapper over `useQuery` and `authQueryOptions`.
13
+ *
14
+ * @param authFn - Better Auth client method (e.g. `authClient.magicLink.list`).
15
+ * @param queryKey - Scope prefix for the key. `options.query` is appended automatically.
16
+ * @param options - Better Auth params (`query`, `fetchOptions`) and React
17
+ * Query options forwarded to `useQuery`.
18
+ */
19
+ export function useAuthQuery<
20
+ TFn extends AuthFn,
21
+ const TQueryKey extends QueryKey
22
+ >(
23
+ authFn: TFn,
24
+ queryKey: TQueryKey,
25
+ options?: UseAuthQueryOptions<TFn, TQueryKey>
26
+ ) {
27
+ const { query, fetchOptions, ...queryOptions } = options ?? {}
28
+
29
+ return useQuery({
30
+ ...authQueryOptions(authFn, queryKey, {
31
+ query,
32
+ fetchOptions
33
+ } as Parameters<TFn>[0]),
34
+ ...queryOptions
35
+ })
36
+ }
package/src/index.ts CHANGED
@@ -4,8 +4,6 @@ export * from "./components/auth/auth-provider"
4
4
  export * from "./components/email"
5
5
  export * from "./components/icons"
6
6
  export * from "./components/settings/account/theme-preview"
7
- export * from "./hooks/auth/use-auth-mutation"
8
- export * from "./hooks/auth/use-auth-query"
9
7
  export * from "./hooks/auth/use-authenticate"
10
8
  export * from "./hooks/auth/use-is-username-available"
11
9
  export * from "./hooks/auth/use-request-password-reset"
@@ -36,6 +34,8 @@ export * from "./hooks/settings/use-revoke-session"
36
34
  export * from "./hooks/settings/use-set-active-session"
37
35
  export * from "./hooks/settings/use-unlink-account"
38
36
  export * from "./hooks/settings/use-update-user"
37
+ export * from "./hooks/use-auth-mutation"
38
+ export * from "./hooks/use-auth-query"
39
39
  export * from "./lib/auth-client"
40
40
  export * from "./lib/auth-config"
41
41
  export * from "./lib/auth-context"
@@ -0,0 +1,18 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for checking username availability.
6
+ *
7
+ * Modeled as a mutation because callers typically trigger the check on
8
+ * user action (debounced input, form submit) rather than on mount.
9
+ *
10
+ * @param authClient - The Better Auth client.
11
+ */
12
+ export function isUsernameAvailableOptions(authClient: AuthClient) {
13
+ return authMutationOptions<
14
+ typeof authClient.isUsernameAvailable,
15
+ ["auth", "isUsernameAvailable"],
16
+ { available: boolean; message: string | null }
17
+ >(authClient.isUsernameAvailable, ["auth", "isUsernameAvailable"])
18
+ }
@@ -0,0 +1,14 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for requesting a password reset email.
6
+ *
7
+ * @param authClient - The Better Auth client.
8
+ */
9
+ export function requestPasswordResetOptions(authClient: AuthClient) {
10
+ return authMutationOptions(authClient.requestPasswordReset, [
11
+ "auth",
12
+ "requestPasswordReset"
13
+ ])
14
+ }
@@ -0,0 +1,14 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for completing a password reset.
6
+ *
7
+ * @param authClient - The Better Auth client.
8
+ */
9
+ export function resetPasswordOptions(authClient: AuthClient) {
10
+ return authMutationOptions(authClient.resetPassword, [
11
+ "auth",
12
+ "resetPassword"
13
+ ])
14
+ }
@@ -0,0 +1,14 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for sending a verification email.
6
+ *
7
+ * @param authClient - The Better Auth client.
8
+ */
9
+ export function sendVerificationEmailOptions(authClient: AuthClient) {
10
+ return authMutationOptions(authClient.sendVerificationEmail, [
11
+ "auth",
12
+ "sendVerificationEmail"
13
+ ])
14
+ }
@@ -0,0 +1,19 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for email/password sign-in.
6
+ *
7
+ * The returned `mutationKey` (`["auth", "signIn", "email"]`) is stable and
8
+ * can be passed to `useIsMutating` or matched inside a global
9
+ * `MutationCache` observer for toast handling.
10
+ *
11
+ * @param authClient - The Better Auth client.
12
+ */
13
+ export function signInEmailOptions(authClient: AuthClient) {
14
+ return authMutationOptions(authClient.signIn.email, [
15
+ "auth",
16
+ "signIn",
17
+ "email"
18
+ ])
19
+ }
@@ -0,0 +1,15 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for magic-link sign-in.
6
+ *
7
+ * @param authClient - The Better Auth client.
8
+ */
9
+ export function signInMagicLinkOptions(authClient: AuthClient) {
10
+ return authMutationOptions(authClient.signIn.magicLink, [
11
+ "auth",
12
+ "signIn",
13
+ "magicLink"
14
+ ])
15
+ }
@@ -0,0 +1,15 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for passkey sign-in.
6
+ *
7
+ * @param authClient - The Better Auth client.
8
+ */
9
+ export function signInPasskeyOptions(authClient: AuthClient) {
10
+ return authMutationOptions(authClient.signIn.passkey, [
11
+ "auth",
12
+ "signIn",
13
+ "passkey"
14
+ ])
15
+ }
@@ -0,0 +1,19 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for social sign-in.
6
+ *
7
+ * The returned `mutationKey` (`["auth", "signIn", "social"]`) is stable and
8
+ * can be passed to `useIsMutating` or matched inside a global
9
+ * `MutationCache` observer for toast handling.
10
+ *
11
+ * @param authClient - The Better Auth client.
12
+ */
13
+ export function signInSocialOptions(authClient: AuthClient) {
14
+ return authMutationOptions(authClient.signIn.social, [
15
+ "auth",
16
+ "signIn",
17
+ "social"
18
+ ])
19
+ }
@@ -0,0 +1,15 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for username/password sign-in.
6
+ *
7
+ * @param authClient - The Better Auth client.
8
+ */
9
+ export function signInUsernameOptions(authClient: AuthClient) {
10
+ return authMutationOptions(authClient.signIn.username, [
11
+ "auth",
12
+ "signIn",
13
+ "username"
14
+ ])
15
+ }
@@ -0,0 +1,11 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for signing out.
6
+ *
7
+ * @param authClient - The Better Auth client.
8
+ */
9
+ export function signOutOptions(authClient: AuthClient) {
10
+ return authMutationOptions(authClient.signOut, ["auth", "signOut"])
11
+ }
@@ -0,0 +1,15 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for email/password sign-up.
6
+ *
7
+ * @param authClient - The Better Auth client.
8
+ */
9
+ export function signUpEmailOptions(authClient: AuthClient) {
10
+ return authMutationOptions(authClient.signUp.email, [
11
+ "auth",
12
+ "signUp",
13
+ "email"
14
+ ])
15
+ }
@@ -0,0 +1,49 @@
1
+ import { type MutationKey, mutationOptions } from "@tanstack/react-query"
2
+ import type { BetterFetchError, BetterFetchOption } from "better-auth/client"
3
+
4
+ export type AuthMutationFn<TData = unknown, TVariables = unknown> = (
5
+ params: TVariables & { fetchOptions?: BetterFetchOption }
6
+ ) => Promise<{ data: TData }>
7
+
8
+ type AuthMutationFnData<TFn> =
9
+ TFn extends AuthMutationFn<infer TData> ? TData : never
10
+
11
+ // biome-ignore lint/suspicious/noExplicitAny: loose constraint for overloaded/generic signatures
12
+ type AuthMutationFnVariables<TFn extends (...args: any) => any> =
13
+ undefined extends Parameters<TFn>[0]
14
+ ? // biome-ignore lint/suspicious/noConfusingVoidType: void lets `mutate()` be called with no arg
15
+ void | NonNullable<Parameters<TFn>[0]>
16
+ : Parameters<TFn>[0]
17
+
18
+ /**
19
+ * Build `mutationOptions` for a Better Auth endpoint.
20
+ *
21
+ * Wires `throw: true` into `fetchOptions` so the mutation rejects with a
22
+ * `BetterFetchError` on failure instead of resolving with `{ error }`.
23
+ *
24
+ * TData is inferred from the endpoint's non-throw response shape. For
25
+ * required-body endpoints where inference falls back to `never`, pass
26
+ * `TData` explicitly (e.g. `authMutationOptions<_, _, { available: boolean }>`).
27
+ *
28
+ * @param authFn - Better Auth client method (e.g. `authClient.signIn.email`).
29
+ * @param mutationKey - Scope for the mutation cache key.
30
+ */
31
+ export function authMutationOptions<
32
+ // biome-ignore lint/suspicious/noExplicitAny: constraint widened so required-body endpoints pass
33
+ TFn extends (...args: any) => any,
34
+ const TMutationKey extends MutationKey,
35
+ TData = AuthMutationFnData<TFn>
36
+ >(authFn: TFn, mutationKey: TMutationKey) {
37
+ return mutationOptions<TData, BetterFetchError, AuthMutationFnVariables<TFn>>(
38
+ {
39
+ mutationKey,
40
+ mutationFn: (variables) => {
41
+ const v = variables as { fetchOptions?: BetterFetchOption } | undefined
42
+ return authFn({
43
+ ...v,
44
+ fetchOptions: { ...v?.fetchOptions, throw: true }
45
+ } as Parameters<TFn>[0]) as Promise<TData>
46
+ }
47
+ }
48
+ )
49
+ }
@@ -0,0 +1,15 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for registering a new passkey.
6
+ *
7
+ * @param authClient - The Better Auth client.
8
+ */
9
+ export function addPasskeyOptions(authClient: AuthClient) {
10
+ return authMutationOptions(authClient.passkey.addPasskey, [
11
+ "auth",
12
+ "passkey",
13
+ "addPasskey"
14
+ ])
15
+ }
@@ -0,0 +1,11 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for changing the current user's email address.
6
+ *
7
+ * @param authClient - The Better Auth client.
8
+ */
9
+ export function changeEmailOptions(authClient: AuthClient) {
10
+ return authMutationOptions(authClient.changeEmail, ["auth", "changeEmail"])
11
+ }
@@ -0,0 +1,14 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for changing the authenticated user's password.
6
+ *
7
+ * @param authClient - The Better Auth client.
8
+ */
9
+ export function changePasswordOptions(authClient: AuthClient) {
10
+ return authMutationOptions(authClient.changePassword, [
11
+ "auth",
12
+ "changePassword"
13
+ ])
14
+ }
@@ -0,0 +1,15 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for deleting a passkey.
6
+ *
7
+ * @param authClient - The Better Auth client.
8
+ */
9
+ export function deletePasskeyOptions(authClient: AuthClient) {
10
+ return authMutationOptions(authClient.passkey.deletePasskey, [
11
+ "auth",
12
+ "passkey",
13
+ "deletePasskey"
14
+ ])
15
+ }
@@ -0,0 +1,11 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for deleting the authenticated user's account.
6
+ *
7
+ * @param authClient - The Better Auth client.
8
+ */
9
+ export function deleteUserOptions(authClient: AuthClient) {
10
+ return authMutationOptions(authClient.deleteUser, ["auth", "deleteUser"])
11
+ }
@@ -0,0 +1,11 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for linking a social provider to the current user.
6
+ *
7
+ * @param authClient - The Better Auth client.
8
+ */
9
+ export function linkSocialOptions(authClient: AuthClient) {
10
+ return authMutationOptions(authClient.linkSocial, ["auth", "linkSocial"])
11
+ }
@@ -0,0 +1,15 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for revoking a device session in multi-session mode.
6
+ *
7
+ * @param authClient - The Better Auth client.
8
+ */
9
+ export function revokeMultiSessionOptions(authClient: AuthClient) {
10
+ return authMutationOptions(authClient.multiSession.revoke, [
11
+ "auth",
12
+ "multiSession",
13
+ "revoke"
14
+ ])
15
+ }
@@ -0,0 +1,14 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for revoking a user session.
6
+ *
7
+ * @param authClient - The Better Auth client.
8
+ */
9
+ export function revokeSessionOptions(authClient: AuthClient) {
10
+ return authMutationOptions(authClient.revokeSession, [
11
+ "auth",
12
+ "revokeSession"
13
+ ])
14
+ }
@@ -0,0 +1,15 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for switching the active device session.
6
+ *
7
+ * @param authClient - The Better Auth client.
8
+ */
9
+ export function setActiveSessionOptions(authClient: AuthClient) {
10
+ return authMutationOptions(authClient.multiSession.setActive, [
11
+ "auth",
12
+ "multiSession",
13
+ "setActive"
14
+ ])
15
+ }
@@ -0,0 +1,14 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for unlinking a social provider from the current user.
6
+ *
7
+ * @param authClient - The Better Auth client.
8
+ */
9
+ export function unlinkAccountOptions(authClient: AuthClient) {
10
+ return authMutationOptions(authClient.unlinkAccount, [
11
+ "auth",
12
+ "unlinkAccount"
13
+ ])
14
+ }
@@ -0,0 +1,15 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authMutationOptions } from "../auth-mutation-options"
3
+
4
+ /**
5
+ * Mutation options factory for updating the authenticated user's profile.
6
+ *
7
+ * The returned `mutationKey` (`["auth", "updateUser"]`) is stable and can
8
+ * be passed to `useIsMutating` or matched inside a global `MutationCache`
9
+ * observer for toast handling.
10
+ *
11
+ * @param authClient - The Better Auth client.
12
+ */
13
+ export function updateUserOptions(authClient: AuthClient) {
14
+ return authMutationOptions(authClient.updateUser, ["auth", "updateUser"])
15
+ }
@@ -0,0 +1,18 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authQueryOptions } from "../auth-query-options"
3
+
4
+ /**
5
+ * Query options factory for the current session.
6
+ *
7
+ * Access `.queryKey` on the returned options for cache seeding or
8
+ * invalidation: `queryClient.setQueryData(sessionOptions(authClient).queryKey, session)`.
9
+ *
10
+ * @param authClient - The Better Auth client.
11
+ * @param params - Parameters forwarded to `authClient.getSession`.
12
+ */
13
+ export function sessionOptions(
14
+ authClient: AuthClient,
15
+ params?: Parameters<AuthClient["getSession"]>[0]
16
+ ) {
17
+ return authQueryOptions(authClient.getSession, ["auth", "getSession"], params)
18
+ }
@@ -0,0 +1,34 @@
1
+ import { type QueryKey, queryOptions } from "@tanstack/react-query"
2
+ import type { BetterFetchError, BetterFetchOption } from "better-auth/client"
3
+
4
+ export type AuthFn<TData = unknown> = (params: {
5
+ query?: Record<string, unknown>
6
+ fetchOptions?: BetterFetchOption
7
+ }) => Promise<{ data: TData }>
8
+
9
+ type AuthFnData<TFn> = TFn extends AuthFn<infer TData> ? TData : never
10
+
11
+ /**
12
+ * Build `queryOptions` for a Better Auth endpoint.
13
+ *
14
+ * Appends `params.query` (or `null`) to the supplied `queryKey` so cache
15
+ * identity tracks endpoint inputs. Wires `throw: true` and the React Query
16
+ * `AbortSignal` into `fetchOptions`.
17
+ *
18
+ * @param authFn - Better Auth client method (e.g. `authClient.getSession`).
19
+ * @param queryKey - Scope prefix for the key. `params.query` is appended automatically.
20
+ * @param params - Parameters forwarded to `authFn`.
21
+ */
22
+ export function authQueryOptions<
23
+ TFn extends AuthFn,
24
+ const TQueryKey extends QueryKey
25
+ >(authFn: TFn, queryKey: TQueryKey, params?: Parameters<TFn>[0]) {
26
+ return queryOptions<AuthFnData<TFn>, BetterFetchError>({
27
+ queryKey: [...queryKey, params?.query ?? null] as const,
28
+ queryFn: ({ signal }) =>
29
+ authFn({
30
+ ...params,
31
+ fetchOptions: { ...params?.fetchOptions, signal, throw: true }
32
+ }) as Promise<AuthFnData<TFn>>
33
+ })
34
+ }
@@ -0,0 +1,25 @@
1
+ import type { AuthClient } from "../../lib/auth-client"
2
+ import { authQueryOptions } from "../auth-query-options"
3
+
4
+ /**
5
+ * Query options factory for provider-specific account info.
6
+ *
7
+ * Keyed per-user. `userId` and `params` are both optional so the factory can
8
+ * build stable options before either is known — the consumer swaps `queryFn`
9
+ * for `skipToken` until ready.
10
+ *
11
+ * @param authClient - The Better Auth client.
12
+ * @param userId - The current signed in user's ID.
13
+ * @param params - Parameters forwarded to `authClient.accountInfo`.
14
+ */
15
+ export function accountInfoOptions(
16
+ authClient: AuthClient,
17
+ userId?: string,
18
+ params?: Parameters<AuthClient["accountInfo"]>[0]
19
+ ) {
20
+ return authQueryOptions(
21
+ authClient.accountInfo,
22
+ ["auth", "user", userId, "accountInfo"],
23
+ params
24
+ )
25
+ }