This commit is contained in:
2023-08-09 22:28:23 +08:00
commit 21483eee26
27 changed files with 13654 additions and 0 deletions

23
src/AuthRoute.tsx Normal file
View File

@@ -0,0 +1,23 @@
import { useMatches, useOutlet } from 'react-router'
import { useMemo } from 'react'
import { Navigate } from 'react-router-dom'
import { getLoginStatus } from '@/utils/auth.ts'
const AuthRoute = () => {
const match = useMatches()[1]
const handle = match.handle as RouteHandle
const outlet = useOutlet()
const isLogin = getLoginStatus()
return useMemo(() => {
if (handle?.auth && !isLogin) {
return <Navigate to="/login" />
}
if (isLogin && match.pathname === '/login') {
return <Navigate to="/" />
}
return outlet
}, [handle?.auth, isLogin, match.pathname, outlet])
}
export default AuthRoute