ConceptPortal-public/rsconcept/frontend/src/components/wrap/RequireAuth.tsx

35 lines
1.1 KiB
TypeScript
Raw Normal View History

'use client';
2024-06-17 21:31:19 +03:00
import { AnimatePresence } from 'framer-motion';
import { useAuth } from '@/context/AuthContext';
2024-06-17 21:31:19 +03:00
import Loader from '../ui/Loader';
2024-03-20 15:27:32 +03:00
import TextURL from '../ui/TextURL';
2024-06-17 21:31:19 +03:00
import AnimateFade from './AnimateFade';
2023-07-15 17:46:19 +03:00
interface RequireAuthProps {
2023-12-28 14:04:44 +03:00
children: React.ReactNode;
2023-07-15 17:46:19 +03:00
}
2023-07-25 20:27:29 +03:00
function RequireAuth({ children }: RequireAuthProps) {
2024-06-17 21:31:19 +03:00
const { user, loading } = useAuth();
return (
<AnimatePresence mode='wait'>
{loading ? <Loader key='auth-loader' /> : null}
{!loading && user ? <AnimateFade key='auth-data'>{children}</AnimateFade> : null}
{!loading && !user ? (
<AnimateFade key='auth-no-user' className='flex flex-col items-center gap-1 mt-2'>
<p className='mb-2'>Пожалуйста войдите в систему</p>
<TextURL text='Войти в Портал' href='/login' />
<TextURL text='Зарегистрироваться' href='/signup' />
<TextURL text='Начальная страница' href='/' />
</AnimateFade>
) : null}
</AnimatePresence>
);
2023-07-15 17:46:19 +03:00
}
2023-12-28 14:04:44 +03:00
export default RequireAuth;