Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
My React HOC logic is showing protected route briefly
import { useRouter } from "next/router";
import { useEffect } from "react";
import axios from "axios";

export default (ChildComponent) => {
  const composeComponent = (props) => {
    const router = useRouter();

    const getLoggedIn = async () => {
      const loggedInRes = await axios.get("http://localhost:5000/api/auth");
      if (loggedInRes.data === false) {
        router.push("/login");
      }
    };

    useEffect(() => {
      getLoggedIn();
    }, []);

    return (
      <div>
        <ChildComponent {...props} />
      </div>
    );
  };
  return composeComponent;
};

I made a GET request to my back-end which will return a false value if user clicks on protected route without being authenticated and then redirect them to the login page. But this logic keep showing the protected route briefly before redirecting the user to the login page. And this is a very bad user experience. Please I would like to know how I can Improve the logic and make sure the user did not see the protected route at all if they are not authenticated and they should also be redirected to the login route.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There are a number of ways you could go but the easiest is perhaps to add another 'isLoaded' state that is false to start with. Hide everything on your page whilst isLoaded is false, then check for auth and after you get a response set isLoaded to true.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!