import { FC, PropsWithChildren, useContext } from 'react'
import { useLocation, Navigate } from 'react-router-dom';
import { Context } from "../../main"
import { observer } from "mobx-react-lite"

const RequireAuth: FC<PropsWithChildren> = observer(({children}) => {

    const {store} = useContext(Context);
    const location = useLocation();

    if (!store.isAuth) {
        return <Navigate to="/login" state={{ from: location }} replace />;
    }

    return children;
})

export default RequireAuth;