judge4c_backup_new/src/features/auth/api/use-logout.ts

35 lines
945 B
TypeScript

import { toast } from "sonner";
import { client } from "@/lib/rpc";
import { InferResponseType } from "hono";
import { useRouter } from "next/navigation";
import { useMutation, useQueryClient } from "@tanstack/react-query";
type ResponseType = InferResponseType<(typeof client.api.auth.logout)["$post"]>;
export const useLogout = () => {
const router = useRouter();
const queryClient = useQueryClient();
const mutation = useMutation<ResponseType, Error>({
mutationFn: async () => {
const response = await client.api.auth.logout["$post"]();
if (!response.ok) {
throw new Error("Failed to logout");
}
return await response.json();
},
onSuccess: () => {
toast.success("Logged out successfully");
router.refresh();
queryClient.invalidateQueries({ queryKey: ["current"] });
},
onError: () => {
toast.error("Failed to logout");
},
});
return mutation;
};