mirror of
https://gitlab.massbug.com/massbug/judge4c.git
synced 2025-07-04 09:51:15 +00:00
35 lines
945 B
TypeScript
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;
|
|
};
|