FastGPT/src/components/Layout/auth.tsx
2023-05-08 09:49:11 +08:00

46 lines
1.1 KiB
TypeScript

import React from 'react';
import { useRouter } from 'next/router';
import { useToast } from '@chakra-ui/react';
import { useUserStore } from '@/store/user';
import { useQuery } from '@tanstack/react-query';
const unAuthPage: { [key: string]: boolean } = {
'/': true,
'/login': true,
'/model/share': true
};
const Auth = ({ children }: { children: JSX.Element }) => {
const router = useRouter();
const toast = useToast({
title: '请先登录',
position: 'top',
status: 'warning'
});
const { userInfo, initUserInfo } = useUserStore();
useQuery(
[router.pathname],
() => {
if (unAuthPage[router.pathname] === true || userInfo) {
return null;
} else {
return initUserInfo();
}
},
{
onError(error) {
console.log('error->', error);
router.replace(
`/login?lastRoute=${encodeURIComponent(location.pathname + location.search)}`
);
toast();
}
}
);
return userInfo || unAuthPage[router.pathname] === true ? children : null;
};
export default Auth;