index.tsx 947 B

123456789101112131415161718192021222324252627282930313233
  1. import {FC, PropsWithChildren} from 'react';
  2. import {HOME_PATH, LOGIN_PATH} from '@routes';
  3. import {useStore} from 'zustand';
  4. import {userStore} from '@stores';
  5. import {useLocation} from 'react-router-dom';
  6. const LDAuthentication: FC<PropsWithChildren> = function({children}) {
  7. const hasParent = window.location !== parent.location;
  8. const token = useStore(userStore, state => state.token);
  9. const {pathname} = useLocation();
  10. const parentRoutes = [HOME_PATH, LOGIN_PATH];
  11. // 判断当前页面是否可以不在iframe中展示
  12. // 只有home和login可以 其他的如果没有parent重定向到首页
  13. if (!parentRoutes.includes(pathname) && !hasParent) {
  14. location.replace(HOME_PATH);
  15. return null;
  16. }
  17. // 判断是否登录
  18. if (!token) {
  19. hasParent
  20. ? parent.location.replace(LOGIN_PATH)
  21. : location.replace(LOGIN_PATH);
  22. return null;
  23. }
  24. return <>{children}</>;
  25. };
  26. export default LDAuthentication;