mirror of
https://github.com/better-auth/better-auth.git
synced 2026-10-19 23:53:17 -05:00
35 lines
899 B
TypeScript
35 lines
899 B
TypeScript
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
|
import { toast } from "sonner";
|
|
import { authClient } from "@/lib/auth-client";
|
|
import { userKeys } from "./keys";
|
|
|
|
interface UpdateUserParams {
|
|
name?: string;
|
|
image?: string;
|
|
}
|
|
|
|
export async function updateUser(params: UpdateUserParams) {
|
|
const { data, error } = await authClient.updateUser(params);
|
|
if (error) throw new Error(error.message);
|
|
|
|
return data;
|
|
}
|
|
export type UpdateUserData = Awaited<ReturnType<typeof updateUser>>;
|
|
|
|
export const useUpdateUserMutation = () => {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: updateUser,
|
|
onSuccess: async () => {
|
|
await queryClient.invalidateQueries({
|
|
queryKey: userKeys.session(),
|
|
});
|
|
toast.success("User updated successfully!");
|
|
},
|
|
onError: (error: any) => {
|
|
toast.error(error.message || "Failed to update user");
|
|
},
|
|
});
|
|
};
|