线上环境首页重定向bug修复

This commit is contained in:
2025-12-24 23:26:30 +08:00
parent 9b9678b0b6
commit 90d9de0e71
3 changed files with 23 additions and 10 deletions
+8 -6
View File
@@ -17,19 +17,20 @@ import useStore from './store/useStore';
const ProtectedRoute = ({ children, requireAuth = false, requireOnboarding = false }) => {
const { isLoggedIn, registrationData } = useStore();
const navigate = useNavigate();
const hasToken = !!localStorage.getItem('access_token');
// 检查是否完成入站流程(有昵称和未来愿景即视为已完成)
const hasCompletedOnboarding = !!(registrationData.nickname && registrationData.future?.vision);
useEffect(() => {
if (requireAuth && !isLoggedIn) {
if (requireAuth && (!isLoggedIn || !hasToken)) {
navigate('/', { replace: true });
} else if (requireOnboarding && !hasCompletedOnboarding) {
navigate('/onboarding', { replace: true });
}
}, [isLoggedIn, hasCompletedOnboarding, requireAuth, requireOnboarding, navigate]);
}, [isLoggedIn, hasCompletedOnboarding, requireAuth, requireOnboarding, hasToken, navigate]);
if (requireAuth && !isLoggedIn) {
if (requireAuth && (!isLoggedIn || !hasToken)) {
return <Loader text="正在验证身份..." />;
}
@@ -66,6 +67,7 @@ const PageTransition = ({ children }) => {
const AnimatedRoutes = () => {
const location = useLocation();
const { isLoggedIn, registrationData } = useStore();
const hasToken = !!localStorage.getItem('access_token');
// 检查是否完成入站流程(有昵称和未来愿景即视为已完成)
const hasCompletedOnboarding = !!(registrationData.nickname && registrationData.future?.vision);
@@ -77,7 +79,7 @@ const AnimatedRoutes = () => {
<Route
path="/"
element={
isLoggedIn ? (
(isLoggedIn && hasToken) ? (
hasCompletedOnboarding ? (
<Navigate to="/dashboard" replace />
) : (
@@ -130,8 +132,8 @@ const AnimatedRoutes = () => {
* App 主组件
*/
function App() {
// 生产环境使用 /course-of-life 作为基础路径
const basename = import.meta.env.PROD ? '/course-of-life' : '';
// 使用 Vite 的 BASE_URL 并移除末尾斜杠,确保 BrowserRouter basename 兼容
const basename = (import.meta.env.BASE_URL || '/').replace(/\/$/, '');
return (
<BrowserRouter basename={basename}>
+7 -3
View File
@@ -60,9 +60,13 @@ api.interceptors.response.use(
localStorage.removeItem('refresh_token');
localStorage.removeItem('life_trajectory_v3'); // 清除 Zustand 持久化状态
// 避免重复跳转导致的无限循环
if (window.location.pathname !== '/') {
window.location.href = '/';
// 避免重复跳转导致的无限循环;使用 Vite 的 BASE_URL 确保在子路径下跳转
const baseUrl = (import.meta.env.BASE_URL || '/');
const normalize = (p) => (p || '/').replace(/\/+$/, '') || '/';
const at = normalize(window.location.pathname);
const target = normalize(baseUrl);
if (at !== target) {
window.location.href = baseUrl; // 仅当不在基路径时跳转
}
return Promise.reject(new Error('未授权访问,请重新登录'));
}