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

306
Views
customization in react auth0

I am using @auth0/auth0-react library to handle authentication process. For an unauthorized user, it will redirect to back to initial screen ("/") route with this query params. ?error=unauthorized... Then again if I am trying to execute the login it will stay on the same page. Maybe I need to find a way to clear the previous auth0 state.

const { loginWithRedirect, logout} = useAuth0();
const loginWithAuth0 = () => {
    logout({}); // clear previous auth 0 state
    loginWithRedirect({
        redirectUri: window.location.origin + "/dashboard"
    });
};

Above is my attempt. Using logout({}) I am trying to clear the previous auth0 state and going forward which doesn't happen. For an authorized user there's no issues. So what I need is to handle the unauthorized user behaviour. Currently it will not redirect to the dashboard which is correct for unauthorized. But what I need is to clear the previous auth0 state and redirect to the next attempt. How can I achieve it?

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

0

You could destructure the isAuthenticated prop off of the object returned by the useAuth0() hook:

const { loginWithRedirect, logout, isAuthenticated } = useAuth0();

if (!isAuthenticated) {
  logout({}); // (not certain this is necessary)
  // redirect with whatever router your framework might include
}

Also consider the withAuthenticationRequired function which is exported from the same @auth0/auth0-react package, and allows you to protect a route/component and configure a redirect for unauthenticated users at the same time:

When you wrap your components in this Higher Order Component and an anonymous user visits your component they will be redirected to the login page and returned to the page they we're redirected from after login.

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!