Auth0 Home Blog Docs

How to trigger autologin with react

jwt
auth0
login
react

#1

I have been following the auth0 react series and it works great but I am wondering if someone can tell me how to set up an autologin of auth0 when the person hits the page. The behavior is that when someone hits a page and they are not logged in, it should redirect to the following page:

class Home extends React.Component {
  componentDidMount() {
    const { user, auth } = this.props;
    if (!auth.isAuthenticated() ) {
      auth.login();
    }
  }

  componentDidUpdate() {
    const { user, auth } = this.props;
    Auth.gotoSingle(user);
  }

  render() {
    return null;
  }
}

The idea is that if they aren’t logged in, call the login method. The problem is that auth0 directs me off the page, so that the componentDidMount triggers after the successful log in which then triggers the auth.login() all over again. What I need to happen is to have the auth.login() to not run when I get redirected back to my page and instead just hang tight for a componentDidUpdate which will call a custom method I have which will redirect to the desired page based on the user’s preference.

I think the main issue, is how can I cause react to just chill for a second after the auth0 login and a redirect to the home page before trying to access the local storage jwt. Any tips on how to do that?

Thanks!