Source

utils/Routes/PublicRoute.jsx

import { Navigate, Outlet } from "react-router-dom";
import ScrollArrow from "../../component/GotoTop/GotoTop";
import Navbar from "../../component/Navbar/Navbar";
/**
 * A route component that redirects authenticated users to the dashboard and displays a public navbar.
 *
 * @returns {JSX.Element} The public route component.
 */
const PublicRoute = () => {
    /**
     * Check if the user is authenticated by looking for a token in local storage.
     * If authenticated, redirect to the dashboard.
     * If not authenticated, render a scroll-to-top button, a navbar, and the nested routes using the Outlet component.
     */
  return localStorage.getItem("token") ? (
    <Navigate to="/dashboard" />
  ) : (
    <>
      <ScrollArrow />
      <Navbar />
      <Outlet />
    </>
  );
};

export default PublicRoute;