fix loop refresh

This commit is contained in:
hamid zarghami
2025-06-01 09:16:31 +03:30
parent 7e12a1eb0a
commit a88a593ddf
4 changed files with 21 additions and 13 deletions
+2 -2
View File
@@ -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}`;
+7 -7
View File
@@ -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,
+9 -1
View File
@@ -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 (
+3 -3
View File
@@ -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: 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 (