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 ChangeEmailParams = Parameters< TAuthClient["changeEmail"] >[0] export type ChangeEmailOptions = Omit< ReturnType>, "mutationKey" | "mutationFn" | "meta" > /** * Mutation options factory for changing the current user's email address. * * @param authClient - The Better Auth client. */ export function changeEmailOptions( authClient: TAuthClient ) { const mutationKey = authMutationKeys.changeEmail const mutationFn = (params: ChangeEmailParams) => authClient.changeEmail({ ...params, fetchOptions: { ...params?.fetchOptions, throw: true } }) return mutationOptions< Awaited>, BetterFetchError, Parameters[0] >({ mutationKey, mutationFn }) } /** * Create a mutation for changing the current user's email address. * * On success, `MutationInvalidator` awaits invalidation of the session * query so the new email is reflected on the user object (see * `meta.awaits`). * * @param authClient - The Better Auth client. * @param options - React Query options forwarded to `useMutation`. */ export function useChangeEmail( authClient: TAuthClient, options?: ChangeEmailOptions, queryClient?: QueryClient ) { return useMutation( { ...changeEmailOptions(authClient), ...options, meta: { awaits: [authQueryKeys.session] } }, queryClient ) }