import React from 'react';
import { useSelector } from 'react-redux';
import { Navigate, useLocation } from 'react-router-dom';

function RequireAuth({ children }) {
  const location = useLocation();
  const auth = useSelector(state => state.auth.token);
  if (!auth) {
    return <Navigate to={'/login'} state={{ from: location.pathname }} />
  }

  return children;
}

export default RequireAuth;