所有模块添加路由守卫
This commit is contained in:
@@ -24,14 +24,6 @@ export default function HomePage() {
|
||||
const [searchParams] = useSearchParams();
|
||||
const [userInfo, setUserInfo] = useState<SessionUserInfo | null>(() => getUserInfo());
|
||||
|
||||
// 登录守卫:未登录跳转 /login
|
||||
useEffect(() => {
|
||||
const token = sessionStorage.getItem('userToken');
|
||||
if (!token) {
|
||||
navigate('/login', { replace: true });
|
||||
}
|
||||
}, [navigate]);
|
||||
|
||||
// 首页模块多时需纵向滚动;离开后恢复全局 overflow:hidden(其他模块自管布局)
|
||||
useEffect(() => {
|
||||
document.body.style.overflow = 'auto';
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useNavigate, useLocation } from 'react-router-dom';
|
||||
import { User, Lock, LogIn, AlertCircle, Loader2 } from 'lucide-react';
|
||||
import StarBackground from '@/shared/components/StarBackground';
|
||||
import { iamLogin, saveSession } from '@/shared/services/iamAuth';
|
||||
@@ -28,6 +28,7 @@ const iconStyle = (isDark: boolean): React.CSSProperties => ({
|
||||
|
||||
export default function LoginPage() {
|
||||
const navigate = useNavigate();
|
||||
const location = useLocation();
|
||||
const { theme } = useTheme();
|
||||
const isDark = theme === 'dark';
|
||||
|
||||
@@ -52,7 +53,9 @@ export default function LoginPage() {
|
||||
try {
|
||||
const result = await iamLogin({ userId: userId.trim(), password });
|
||||
saveSession(result.token, result.userInfo);
|
||||
navigate('/', { replace: true });
|
||||
// 回跳原页面(RequireAuth 记录的 from),否则去首页
|
||||
const from = (location.state as { from?: string })?.from ?? '/';
|
||||
navigate(from, { replace: true });
|
||||
} catch (ex) {
|
||||
setError(ex instanceof Error ? ex.message : '登录失败,请稍后重试');
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user