From 662e376d7265f49c341ad37bce2361922b5b543d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E9=98=B3?= <3311118881@qq.com> Date: Sun, 4 Aug 2024 19:20:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=B7=B2=E7=9F=A5=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useAuthRedirect.tsx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/hooks/useAuthRedirect.tsx b/src/hooks/useAuthRedirect.tsx index 59e09db..495d76c 100644 --- a/src/hooks/useAuthRedirect.tsx +++ b/src/hooks/useAuthRedirect.tsx @@ -2,7 +2,7 @@ import { useEffect } from 'react'; import { useNavigate, useLocation } from 'react-router-dom'; import { useUserStore } from '@/stores'; -// 如果有token,就跳转到首页 +// 如果即将去往的是 /login 页面并且有 token 情况就自动重定向到 /home,否则就跳转 const useAuthRedirect = () => { const store = useUserStore(); const navigate = useNavigate(); @@ -10,8 +10,10 @@ const useAuthRedirect = () => { useEffect(() => { const token = store.token; - if (token) navigate('/home'); - }, [location, navigate]); + const isGoingToLogin = location.pathname === '/login'; + + if (isGoingToLogin && token) navigate('/home'); + }, [location, navigate, store.token]); }; export default useAuthRedirect;