36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { AuthActionResponse, useAuth } from "@/lib/hooks";
|
|
import { UseMutationOptions, useMutation } from "@tanstack/react-query";
|
|
import { useNavigate } from "react-router-dom";
|
|
import { toast } from "react-toastify";
|
|
import { useQueryKey } from "../useQueryKey";
|
|
|
|
export const useLogout = (params?: UseMutationOptions<AuthActionResponse, Error>) => {
|
|
const { onSuccess, onError, ...restParams } = params || {};
|
|
const keys = useQueryKey();
|
|
const { logout } = useAuth();
|
|
const navigate = useNavigate();
|
|
|
|
return useMutation({
|
|
mutationKey: keys().auth().action("logout").get(),
|
|
mutationFn: logout,
|
|
onSuccess: async (data, variables, context) => {
|
|
const { success, redirectTo } = data;
|
|
if (success && redirectTo) {
|
|
navigate(redirectTo);
|
|
}
|
|
if (onSuccess) {
|
|
onSuccess(data, variables, context);
|
|
}
|
|
},
|
|
onError: (error, variables, context) => {
|
|
const { message } = error;
|
|
toast.error(message);
|
|
|
|
if (onError) {
|
|
onError(error, variables, context);
|
|
}
|
|
},
|
|
...restParams,
|
|
});
|
|
};
|