// REQ-USR-004: /login 守卫(BR2)。已登录访问登录页 → 回 from 或 /。 import type { ReactNode } from 'react'; import { Navigate, useLocation } from 'react-router-dom'; import { useAppSelector } from '../store/hooks'; interface RedirectIfAuthedProps { children: ReactNode; } /** * 已登录态判定(D:仅 token 即视为已登录,与 RequireAuth 的「token 存在即非未登录」语义对齐; * 避免 token 已持久化、user 尚在拉取时登录页与守卫之间互相弹跳)。 * 已登录 → 重定向到 location.state.from(来源)或 /;否则渲染 children(LoginPage)。 */ export default function RedirectIfAuthed({ children }: RedirectIfAuthedProps) { const token = useAppSelector((s) => s.auth.token); const location = useLocation(); if (token) { const from = (location.state as { from?: string } | null)?.from; return ; } return <>{children}; }