所有模块添加路由守卫

This commit is contained in:
2026-08-27 16:46:52 +08:00
parent 989120cab8
commit cd5a385cda
4 changed files with 55 additions and 20 deletions
+5 -2
View File
@@ -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 {