From 28d401ec01dea0a62358ee0b9e1e189fe76b21dd Mon Sep 17 00:00:00 2001 From: "Apple\\Apple" Date: Tue, 20 May 2025 11:53:04 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20Redirect=20to=20console=20if?= =?UTF-8?q?=20logged=20in=20and=20accessing=20auth=20pages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit introduces a new component `AuthRedirect` which checks if a user is already logged in. If the user is logged in and attempts to access the /login or /register pages, they will be redirected to the /console page. Otherwise, the respective authentication form (Login or Register) will be rendered. The `App.js` file has been updated to utilize this new `AuthRedirect` component for the /login and /register routes. --- web/src/App.js | 9 +++++++-- web/src/components/AuthRedirect.js | 14 ++++++++++++++ web/src/pages/Home/index.js | 2 +- 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 web/src/components/AuthRedirect.js diff --git a/web/src/App.js b/web/src/App.js index bf5b69c4..7119ef18 100644 --- a/web/src/App.js +++ b/web/src/App.js @@ -27,6 +27,7 @@ import OAuth2Callback from './components/OAuth2Callback.js'; import PersonalSetting from './components/PersonalSetting.js'; import Setup from './pages/Setup/index.js'; import SetupCheck from './components/SetupCheck'; +import AuthRedirect from './components/AuthRedirect'; const Home = lazy(() => import('./pages/Home')); const Detail = lazy(() => import('./pages/Detail')); @@ -138,7 +139,9 @@ function App() { path='/login' element={ } key={location.pathname}> - + + + } /> @@ -146,7 +149,9 @@ function App() { path='/register' element={ } key={location.pathname}> - + + + } /> diff --git a/web/src/components/AuthRedirect.js b/web/src/components/AuthRedirect.js new file mode 100644 index 00000000..d22c675d --- /dev/null +++ b/web/src/components/AuthRedirect.js @@ -0,0 +1,14 @@ +import React from 'react'; +import { Navigate } from 'react-router-dom'; + +const AuthRedirect = ({ children }) => { + const user = localStorage.getItem('user'); + + if (user) { + return ; + } + + return children; +}; + +export default AuthRedirect; \ No newline at end of file diff --git a/web/src/pages/Home/index.js b/web/src/pages/Home/index.js index 7c151467..e67a4ed7 100644 --- a/web/src/pages/Home/index.js +++ b/web/src/pages/Home/index.js @@ -94,7 +94,7 @@ const Home = () => { {/* 操作按钮 */}
- +