67 lines
1.9 KiB
JavaScript
Executable File
67 lines
1.9 KiB
JavaScript
Executable File
// src/layouts/Auth.js
|
|
import React from 'react';
|
|
import { Routes, Route, Navigate } from 'react-router-dom';
|
|
import { Box } from '@chakra-ui/react';
|
|
import { useAuth } from '../contexts/AuthContext';
|
|
import ErrorBoundary from '../components/ErrorBoundary';
|
|
|
|
// 导入认证相关页面
|
|
import SignInIllustration from '../views/Authentication/SignIn/SignInIllustration';
|
|
import SignUpIllustration from '../views/Authentication/SignUp/SignUpIllustration';
|
|
|
|
// 认证路由组件 - 已登录用户不能访问登录页
|
|
const AuthRoute = ({ children }) => {
|
|
const { isAuthenticated, isLoading } = useAuth();
|
|
|
|
// 加载中不做跳转
|
|
if (isLoading) {
|
|
return children;
|
|
}
|
|
|
|
// 已登录用户跳转到首页
|
|
if (isAuthenticated) {
|
|
// 检查是否有记录的重定向路径
|
|
const redirectPath = localStorage.getItem('redirectPath');
|
|
if (redirectPath && redirectPath !== '/auth/signin' && redirectPath !== '/auth/sign-up') {
|
|
localStorage.removeItem('redirectPath');
|
|
return <Navigate to={redirectPath} replace />;
|
|
}
|
|
return <Navigate to="/home" replace />;
|
|
}
|
|
|
|
return children;
|
|
};
|
|
|
|
export default function Auth() {
|
|
return (
|
|
<ErrorBoundary>
|
|
<Box minH="100vh">
|
|
<Routes>
|
|
{/* 登录页面 */}
|
|
<Route
|
|
path="/signin"
|
|
element={
|
|
<AuthRoute>
|
|
<SignInIllustration />
|
|
</AuthRoute>
|
|
}
|
|
/>
|
|
|
|
{/* 注册页面 */}
|
|
<Route
|
|
path="/sign-up"
|
|
element={
|
|
<AuthRoute>
|
|
<SignUpIllustration />
|
|
</AuthRoute>
|
|
}
|
|
/>
|
|
|
|
{/* 默认重定向到登录页 */}
|
|
<Route path="/" element={<Navigate to="/auth/signin" replace />} />
|
|
<Route path="*" element={<Navigate to="/auth/signin" replace />} />
|
|
</Routes>
|
|
</Box>
|
|
</ErrorBoundary>
|
|
);
|
|
} |