From a88a593ddfa808f9c670b5f4e051c11204fab1fd Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Sun, 1 Jun 2025 09:16:31 +0330 Subject: [PATCH] fix loop refresh --- src/config/axios.ts | 4 ++-- src/config/func.ts | 14 +++++++------- src/shared/Header.tsx | 10 +++++++++- src/shared/SideBarItem.tsx | 6 +++--- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/config/axios.ts b/src/config/axios.ts index 91a0612..9919890 100644 --- a/src/config/axios.ts +++ b/src/config/axios.ts @@ -39,8 +39,8 @@ axiosInstance.interceptors.response.use( try { const refreshTokenValue = await getRefreshToken(); if (!refreshTokenValue) { - removeRefreshToken(); - removeToken(); + await removeRefreshToken(); + await removeToken(); const slug = localStorage.getItem(import.meta.env.VITE_SLUG_ID); const slug_parse = slug ? JSON.parse(slug) : null; window.location.href = `/${slug_parse?.slug}`; diff --git a/src/config/func.ts b/src/config/func.ts index ba41302..ce7171d 100644 --- a/src/config/func.ts +++ b/src/config/func.ts @@ -32,6 +32,9 @@ export const timeAgo = (date: string | Date): string => { return "همین الان"; }; +const isProduction = import.meta.env.MODE === "production"; +const domain = window.location.hostname; + export const getToken = async () => { return Cookie.get(import.meta.env.VITE_TOKEN_NAME); }; @@ -40,10 +43,7 @@ export const getRefreshToken = async () => { return Cookie.get(import.meta.env.VITE_REFRESH_TOKEN_NAME); }; -const isProduction = import.meta.env.MODE === "production"; -const domain = window.location.hostname; - -export const setToken = (token: string) => { +export const setToken = async (token: string) => { Cookie.set(import.meta.env.VITE_TOKEN_NAME, token, { domain, secure: isProduction, @@ -51,7 +51,7 @@ export const setToken = (token: string) => { }); }; -export const setRefreshToken = (refreshToken: string) => { +export const setRefreshToken = async (refreshToken: string) => { Cookie.set(import.meta.env.VITE_REFRESH_TOKEN_NAME, refreshToken, { domain, secure: isProduction, @@ -59,7 +59,7 @@ export const setRefreshToken = (refreshToken: string) => { }); }; -export const removeToken = () => { +export const removeToken = async () => { Cookie.remove(import.meta.env.VITE_TOKEN_NAME, { domain, secure: isProduction, @@ -67,7 +67,7 @@ export const removeToken = () => { }); }; -export const removeRefreshToken = () => { +export const removeRefreshToken = async () => { Cookie.remove(import.meta.env.VITE_REFRESH_TOKEN_NAME, { domain, secure: isProduction, diff --git a/src/shared/Header.tsx b/src/shared/Header.tsx index 6f25dfa..0d17fe9 100644 --- a/src/shared/Header.tsx +++ b/src/shared/Header.tsx @@ -16,7 +16,11 @@ import { getToken } from '../config/func' const Header: FC = () => { - const isLogin = !!getToken() + const [isLogin, setIsLogin] = useState(false) + const checkLogin = async () => { + const token = await getToken() + setIsLogin(!!token) + } const { slug } = useParams() const navigate = useNavigate() @@ -29,6 +33,10 @@ const Header: FC = () => { useEffect(() => { setPopoverKey((prevKey) => prevKey + 1); + const check = async () => { + await checkLogin() + } + check() }, [location.pathname]); return ( diff --git a/src/shared/SideBarItem.tsx b/src/shared/SideBarItem.tsx index 5d951df..fe5faba 100644 --- a/src/shared/SideBarItem.tsx +++ b/src/shared/SideBarItem.tsx @@ -1,7 +1,6 @@ import { FC, ReactNode } from 'react' -import { Link } from 'react-router-dom' +import { Link, useParams } from 'react-router-dom' import { buildPath, clx } from '../helpers/utils' -import { Pages } from '../config/Pages' import { useLogout } from '../pages/auth/hooks/useAuthData' import { removeToken } from '../config/func' import { removeRefreshToken } from '../config/func' @@ -17,13 +16,14 @@ type Props = { const SideBarItem: FC = (props: Props) => { + const { slug } = useParams() const logout = useLogout() const handleLogout = async () => { await logout.mutateAsync() removeToken() removeRefreshToken() - window.location.href = Pages.auth.login + window.location.href = buildPath('', slug) } return (