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

141
Views
Render an authentication protected component after an async authentication

Expected: the async function checks if the user is authenticated, then return true, so that the protected component gets rendered, or false, that redirects the user to the login page.

What actually happens: the getAuth() function returns "Promise ", breaking the code.

  export default function RequireAuth({ children, redirectTo }) {
  const BASE_API_URL = "https://api-backend.test";

  const getAuth = async () => {
    const isAuth = await axios
      .get(BASE_API_URL + "/user_auth.php", {
        withCredentials: true,
      })
      .then((res) => {
        if (res.status === 201) {
          return true;
        }
      })
      .catch((err) => {
        if (err.response.status === 401) {
          return false;
        }
      });

    return isAuth;
  };

  let isAuthenticated = getAuth();
  return isAuthenticated ? children : <Navigate to={redirectTo} />;
}

This is how the "protected" component should be displayed according to the official documentation here.

<Route
  path="/dashboard"
  element={
    <RequireAuth redirectTo="/login">
      <Dashboard />
    </RequireAuth>
  }
/>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You have to do it properly using useEffect, try this

export default function RequireAuth({ children, redirectTo }) {
  const BASE_API_URL = "https://api-backend.test";
  const [isAuthenticated, setAuthenticated] = useState(false);

  useEffect(() => {
    const getAuth = () => {
      const isAuth = axios
        .get(BASE_API_URL + "/user_auth.php", {
          withCredentials: true
        })
        .then((res) => {
          if (res.status === 201) {
            setAuthenticated(true);
          }
        })
        .catch((err) => {
          if (err.response.status === 401) {
            setAuthenticated(false);
          }
        });
    };

    getAuth();
  }, [redirectTo]);

  if (isAuthenticated) {
    return children;
  }
  return <Navigate to={redirectTo} />;
}
about 4 years ago · Juan Pablo Isaza Report

0

getAuth is an async function, so it will return a Promise. You have to either await its output or use .then to work with the resolved promise.

Side note, it's usually preferable to either use async/await or .then, but not both.

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!