2023-06-30 14:17:38 +03:00
|
|
|
import { useRouter } from "next/navigation";
|
|
|
|
import { useState } from "react";
|
2023-08-07 15:13:41 +03:00
|
|
|
import { useTranslation } from "react-i18next";
|
2023-06-30 14:17:38 +03:00
|
|
|
|
|
|
|
import { useSupabase } from "@/lib/context/SupabaseProvider";
|
|
|
|
import { useToast } from "@/lib/hooks";
|
|
|
|
import { useEventTracking } from "@/services/analytics/useEventTracking";
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
export const useLogout = () => {
|
|
|
|
const { supabase } = useSupabase();
|
|
|
|
const [isPending, setIsPending] = useState(false);
|
|
|
|
const { track } = useEventTracking();
|
|
|
|
|
2023-08-07 15:13:41 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
const {t, i18n} = useTranslation(["translation","logout"]);
|
|
|
|
|
2023-06-30 14:17:38 +03:00
|
|
|
const { publish } = useToast();
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
const handleLogout = async () => {
|
|
|
|
setIsPending(true);
|
|
|
|
const { error } = await supabase.auth.signOut();
|
|
|
|
void track("LOGOUT");
|
2023-07-18 19:28:44 +03:00
|
|
|
localStorage.clear();
|
|
|
|
|
2023-06-30 14:17:38 +03:00
|
|
|
if (error) {
|
|
|
|
console.error("Error logging out:", error.message);
|
|
|
|
publish({
|
|
|
|
variant: "danger",
|
2023-08-07 15:13:41 +03:00
|
|
|
text: t("error", { errorMessage: error.message, ns: "logout"}),
|
2023-06-30 14:17:38 +03:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
publish({
|
|
|
|
variant: "success",
|
2023-08-07 15:13:41 +03:00
|
|
|
text: t("loggedOut", {ns : "logout"})
|
2023-06-30 14:17:38 +03:00
|
|
|
});
|
|
|
|
router.replace("/");
|
|
|
|
}
|
|
|
|
setIsPending(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
handleLogout,
|
|
|
|
isPending,
|
|
|
|
};
|
|
|
|
};
|