import { authMutationKeys } from "@better-auth-ui/core" import { mutationOptions, useMutation } from "@tanstack/react-query" import type { BetterFetchError } from "better-auth/react" import type { AuthClient } from "../../lib/auth-client" export type ResetPasswordParams = Parameters< TAuthClient["resetPassword"] >[0] export type ResetPasswordOptions = Omit< ReturnType>, "mutationKey" | "mutationFn" > /** * Mutation options factory for completing a password reset. * * @param authClient - The Better Auth client. */ export function resetPasswordOptions( authClient: TAuthClient ) { const mutationKey = authMutationKeys.resetPassword const mutationFn = (params: ResetPasswordParams) => authClient.resetPassword({ ...params, fetchOptions: { ...params?.fetchOptions, throw: true } }) return mutationOptions< Awaited>, BetterFetchError, Parameters[0] >({ mutationKey, mutationFn }) } /** * Create a mutation for completing a password reset. * * Wraps `authClient.resetPassword` (using the token from the reset email) * and forwards React Query mutation options such as `onSuccess`, `onError`, * and `retry`. * * @param authClient - The Better Auth client. * @param options - React Query options forwarded to `useMutation`. */ export function useResetPassword( authClient: TAuthClient, options?: ResetPasswordOptions ) { return useMutation({ ...resetPasswordOptions(authClient), ...options }) }