Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
596 B
9
Indexable
Never
import { useAuthenticationStatus } from "@nhost/react";
import { Navigate, useLocation } from "react-router-dom";

const ProtectedRoute = ({ children }) => {
  const { isAuthenticated, isLoading } = useAuthenticationStatus();
  const location = useLocation();

  if (isLoading) {
    console.log("protected inside", isAuthenticated);
    return (
      <div>
        <p>Please wait....</p>
      </div>
    );
  }

  if (!isAuthenticated) {
    return <Navigate to="/login" state={{ from: location }} replace />;
  }

  return children;
};

export default ProtectedRoute;