import React from "react";
import { Navigate, useLocation } from "react-router-dom";
import { useAppSelector } from "../app/hooks";

interface IAuthRedirect {
  isAuthenticated: boolean;
  to: string;
}

const AuthRedirect: React.FC<IAuthRedirect> = ({ children , isAuthenticated, to}) => {
  const authenticated = useAppSelector(state => state.auth.authenticated);

  const { pathname } = useLocation();

  if (authenticated == isAuthenticated) return (<>{children}</>);
  return (<Navigate to={to} replace state={{ path: pathname }} />);
};

export default AuthRedirect;
