import { authMutationKeys, authQueryKeys } from "@better-auth-ui/core" import { mutationOptions, type QueryClient, useMutation } from "@tanstack/react-query" import type { BetterFetchError } from "better-auth/react" import type { AuthClient } from "../../lib/auth-client" export type UpdateUserParams = Parameters< TAuthClient["updateUser"] >[0] export type UpdateUserOptions = Omit< ReturnType>, "mutationKey" | "mutationFn" | "meta" > /** * Mutation options factory for updating the authenticated user's profile. * * @param authClient - The Better Auth client. */ export function updateUserOptions( authClient: TAuthClient ) { const mutationKey = authMutationKeys.updateUser const mutationFn = (params: UpdateUserParams) => authClient.updateUser({ ...params, fetchOptions: { ...params?.fetchOptions, throw: true } }) return mutationOptions< Awaited>, BetterFetchError, Parameters[0] >({ mutationKey, mutationFn }) } /** * Create a mutation for updating the authenticated user's profile. * * On success, `MutationInvalidator` awaits invalidation of the session * query so the updated user fields are reflected (see `meta.awaits`). * * @param authClient - The Better Auth client. * @param options - React Query options forwarded to `useMutation`. */ export function useUpdateUser( authClient: TAuthClient, options?: UpdateUserOptions, queryClient?: QueryClient ) { return useMutation( { ...updateUserOptions(authClient), ...options, meta: { awaits: [authQueryKeys.session] } }, queryClient ) }