{"size": 957, "ext": "js", "max_stars_repo_path": "client/src/components/PrivateRoute.js", "max_stars_repo_name": "ironhack-project-three/ironhack-project-three", "max_stars_repo_stars_event_min_datetime": "2021-10-11T12:37:41.000Z", "max_stars_repo_stars_event_max_datetime": "2021-10-11T12:37:41.000Z", "max_issues_repo_path": "client/src/components/PrivateRoute.js", "max_issues_repo_name": "Theadejong/VineVibe", "max_issues_repo_issues_event_min_datetime": "2021-10-05T08:32:53.000Z", "max_issues_repo_issues_event_max_datetime": "2021-10-19T22:19:58.000Z", "max_forks_repo_path": "client/src/components/PrivateRoute.js", "max_forks_repo_name": "Theadejong/VineVibe", "max_forks_repo_forks_event_min_datetime": "2021-10-05T08:33:32.000Z", "max_forks_repo_forks_event_max_datetime": "2021-10-17T17:19:32.000Z"}
{"max_stars_count": 1, "max_issues_count": 24, "max_forks_count": 6, "avg_line_length": 23.925, "max_line_length": 70, "alphanum_fraction": 0.6812957158}
import PropTypes from 'prop-types';
import { useContext } from "react";
import { Redirect, Route } from "react-router-dom";
import React from "react";

import { AuthContext } from "../context/auth.context";

function PrivateRoute(props) {
  // Destructure the props
  const {
    toRedirect = "/login",
    exact,
    component: Component,
    ...restProps
  } = props;

  const { isLoggedIn, isLoading } = useContext(AuthContext);

  // If the authentication is still loading ⏳
  if (isLoading) return <p>Loading ...</p>;

  // If the user is not logged in ❌
  if (!isLoggedIn) return <Redirect to={toRedirect} />;

  // If the user is logged in ✅
  return <Route exact={exact} component={Component} {...restProps} />;
}

PrivateRoute.propTypes = {
  toRedirect: PropTypes.string,
  exact: PropTypes.bool,
  component: PropTypes.oneOfType([
    PropTypes.arrayOf(PropTypes.node),
    PropTypes.node,
    PropTypes.func
  ])
}

export default PrivateRoute;
