Portal/rsconcept/frontend/src/components/wrap/RequireAuth.tsx

35 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
'use client';
2024-06-17 21:30:58 +03:00
import { AnimatePresence } from 'framer-motion';
2024-06-07 20:17:03 +03:00
import { useAuth } from '@/context/AuthContext';
2024-06-17 21:30:58 +03:00
import Loader from '../ui/Loader';
2024-06-07 20:17:03 +03:00
import TextURL from '../ui/TextURL';
2024-06-17 21:30:58 +03:00
import AnimateFade from './AnimateFade';
2024-06-07 20:17:03 +03:00
interface RequireAuthProps {
children: React.ReactNode;
}
function RequireAuth({ children }: RequireAuthProps) {
2024-06-17 21:30:58 +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>
);
2024-06-07 20:17:03 +03:00
}
export default RequireAuth;