diff --git a/src/components/Layout/Header.jsx b/src/components/Layout/Header.jsx index 6e05c1e1..41c7ce03 100644 --- a/src/components/Layout/Header.jsx +++ b/src/components/Layout/Header.jsx @@ -19,7 +19,7 @@ import { useProjectName } from "../../hooks/useProjects"; import eventBus from "../../services/eventBus"; import { useHasUserPermission } from "../../hooks/useHasUserPermission"; import { MANAGE_PROJECT } from "../../utils/constants"; -import { useAuthModal } from "../../hooks/useAuth"; +import { useAuthModal, useLogout } from "../../hooks/useAuth"; const Header = () => { const { profile } = useProfile(); @@ -29,9 +29,7 @@ const Header = () => { const navigate = useNavigate(); const {onOpen} = useAuthModal() const HasManageProjectPermission = useHasUserPermission(MANAGE_PROJECT); - // { - // console.log(location.pathname); - // } + const { mutate : logout,isPending:logouting} = useLogout() const isDashboardPath = /^\/dashboard$/.test(location.pathname) || /^\/$/.test(location.pathname); @@ -61,41 +59,9 @@ const Header = () => { return role ? role.name : "User"; }; - const handleLogout = (e) => { - e.preventDefault(); - logout(); - }; + - const logout = async () => { - try { - let data = { - refreshToken: localStorage.getItem("refreshToken"), - }; - - AuthRepository.logout(data) - .then(() => { - localStorage.removeItem("jwtToken"); - localStorage.removeItem("refreshToken"); - localStorage.removeItem("user"); - localStorage.clear(); - clearAllCache(); - window.location.href = "/auth/login"; - }) - .catch(() => { - localStorage.removeItem("jwtToken"); - localStorage.removeItem("refreshToken"); - localStorage.removeItem("user"); - localStorage.clear(); - clearAllCache(); - window.location.href = "/auth/login"; - }); - } catch (error) { - console.error( - "Error during logout:", - error?.response?.data || error.message - ); - } - }; + const handleProfilePage = () => { navigate(`/employee/${profile?.employeeInfo?.id}`); @@ -474,11 +440,10 @@ const Header = () => { handleLogout()} > - - Log Out + {logouting ? "Please Wait":<> + SignOut} diff --git a/src/hooks/useAuth.jsx b/src/hooks/useAuth.jsx index 2790558d..8f158197 100644 --- a/src/hooks/useAuth.jsx +++ b/src/hooks/useAuth.jsx @@ -53,4 +53,31 @@ export const useAuthModal = () => { onOpen: () => dispatch(openAuthModal()), onClose: () => dispatch(closeAuthModal()), }; -}; \ No newline at end of file +}; + +export const useLogout = ()=>{ + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: async () => { + let payload = {refreshToken: localStorage.getItem("refreshToken")} + return await AuthRepository.logout(payload); + }, + + onSuccess: (data) => { + localStorage.removeItem("jwtToken"); + localStorage.removeItem("refreshToken"); + localStorage.removeItem("ctnt"); + localStorage.clear(); + window.location.href = "/auth/login"; + if (onSuccessCallBack) onSuccessCallBack(); + }, + + onError: (error) => { + showToast(error.message || "Error while creating project", "error"); + localStorage.removeItem("jwtToken"); + localStorage.removeItem("refreshToken") + localStorage.removeItem("ctnt") + }, + }); +} \ No newline at end of file diff --git a/src/pages/authentication/SwitchTenant.jsx b/src/pages/authentication/SwitchTenant.jsx index e07a3416..b5a8652a 100644 --- a/src/pages/authentication/SwitchTenant.jsx +++ b/src/pages/authentication/SwitchTenant.jsx @@ -21,6 +21,7 @@ const SwitchTenant = () => { queryKey: ["profile"], queryFn: () => AuthRepository.profile(), }); + window.location.reload(); }); const currentTenant = localStorage.getItem("ctnt"); const handleTenantselection = (tenantId) => { @@ -83,11 +84,11 @@ const SwitchTenant = () => { )} diff --git a/src/pages/authentication/TenantSelectionPage.jsx b/src/pages/authentication/TenantSelectionPage.jsx index a8771488..a628e683 100644 --- a/src/pages/authentication/TenantSelectionPage.jsx +++ b/src/pages/authentication/TenantSelectionPage.jsx @@ -1,5 +1,5 @@ import { useEffect, useState } from "react"; -import { useTenants, useSelectTenant } from "../../hooks/useAuth.jsx"; +import { useTenants, useSelectTenant, useLogout } from "../../hooks/useAuth.jsx"; import { Link, useNavigate } from "react-router-dom"; import Dashboard from "../../components/Dashboard/Dashboard.jsx"; import Loader from "../../components/common/Loader.jsx"; @@ -17,12 +17,15 @@ const TenantSelectionPage = () => { localStorage.setItem("ctnt", tenantId); chooseTenant(tenantId); }; + + const {mutate:handleLogout,isPending:isLogouting} = useLogout(()=>{}) - useEffect(() => { - if (localStorage.getItem("ctnt")) { - navigate("/dashboard"); - } - }, [navigate]); + + // useEffect(() => { + // if (localStorage.getItem("ctnt")) { + // navigate("/dashboard"); + // } + // }, [navigate]); useEffect(() => { if (!isLoading && data?.data?.length === 1) { @@ -47,7 +50,7 @@ const TenantSelectionPage = () => { return ( -
+
{/* Logo */}
{

Please select which dashboard you want to explore!!!

+
handleLogout()}> + {isLogouting ? "Please Wait...":SignOut} +
+
{/* Card Section */}