Files
oxygen-ui/src/AuthRoute.tsx
2023-09-03 16:06:20 +08:00

21 lines
556 B
TypeScript

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