Files
FastGPT/projects/app/src/components/Layout/auth.tsx

54 lines
1.4 KiB
TypeScript

import { useRouter } from 'next/router';
import { useUserStore } from '@/web/support/user/useUserStore';
import { useQuery } from '@tanstack/react-query';
import { useTranslation } from 'next-i18next';
import { useToast } from '@fastgpt/web/hooks/useToast';
const unAuthPage: { [key: string]: boolean } = {
'/': true,
'/login': true,
'/login/provider': true,
'/login/fastlogin': true,
'/login/sso': true,
'/appStore': true,
'/chat': true,
'/chat/share': true,
'/tools/price': true,
'/price': true
};
const Auth = ({ children }: { children: JSX.Element | React.ReactNode }) => {
const { t } = useTranslation();
const router = useRouter();
const { toast } = useToast();
const { userInfo, initUserInfo } = useUserStore();
useQuery(
[router.pathname],
() => {
if (unAuthPage[router.pathname] === true) {
return null;
} else {
return initUserInfo();
}
},
{
refetchInterval: 10 * 60 * 1000,
onError(error) {
console.log('error->', error);
router.replace(
`/login?lastRoute=${encodeURIComponent(location.pathname + location.search)}`
);
toast({
status: 'warning',
title: t('common:support.user.Need to login')
});
}
}
);
return !!userInfo || unAuthPage[router.pathname] === true ? children : null;
};
export default Auth;