Source

utils/Routes/PrivateRoute.jsx

import { Outlet, Navigate } from "react-router-dom";
/**
 * A private route component that renders its children (nested routes) if a user is authenticated.
 * If the user is not authenticated, it redirects to the home page.
 *
 * @returns {JSX.Element} The private route component.
 */
const PrivateRoute = () => {
  /**
   * Check if the user is authenticated by looking for a token in local storage.
   * If a token is found, render the nested routes using the Outlet component.
   * If not authenticated, redirect to the home page.
   */
  return localStorage.getItem("token") ? <Outlet /> : <Navigate to="/" />;
};

export default PrivateRoute;