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

307
Views
How to direct user to another page if the person not authorized?

I want to direct user to the login page if he/she is not authorized. I wrote the bottom code for this purpose. Root route is login page, and AdminPanel page is for admin. If data.success is true this means person is admin. But this code render 404 not found on http://localhost:3000/user url. How can I fix this issue?

const App = () => {
  const [auth, setAuth] = useState(false);
  const authControl = async () => {
    try {
      const res = await axios({
        url: "https://localhost:44357/api/Auth/user",
        withCredentials: true,
      });
      console.log(res.data.success);
      if (res.data.success) setAuth(true);
    } catch (err) {
      console.log(err);
    }
  };
  useEffect(() => {
    authControl();
  }, []);
  return (
    <div>
      <BrowserRouter>
        <Route path="/" exact component={Login} />
        <Route
          path="/user"
          render={() => {
            auth ? <AdminPanel /> : <Redirect to="/" />;
          }}
        />
        <Route render={() => <h1>404 Not Found</h1>} />
      </BrowserRouter>
    </div>
  );
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you need to wrap the <Route />s in a <Switch /> from react router. something like this:

 <Switch>
    <Route path="/public">
        <PublicPage />
    </Route>
    <Route path="/login">
        <LoginPage />
    </Route>
</Switch>
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!