auth0-blog / react-flux-jwt-authentication-sample

Sample for implementing Authentication with a React Flux app and JWTs
590 stars 101 forks source link

Make the dispatch before transitioning to an authenticated component #22

Closed dopeboy closed 9 years ago

dopeboy commented 9 years ago

Suppose "Dashboard" is an authenticated component. Suppose the user has not authenticated and attempts to go to example.com/dashboard. The code will correctly kick them to example.com/login?nextPath=%2Fdashboard.

Once they login successfully, RouterContainer.get().transitionTo(nextPath) eventually runs. This triggers the following code in in AuthenticatedComponent.jsx to run:

    static willTransitionTo(transition) {
      if (!LoginStore.isLoggedIn()) {
        transition.redirect('/login', {}, {'nextPath' : transition.path});
      }
    }

The if condition will be true because the dispatcher has not updated the store yet. The net effect is the transition to "example.com/dashboard" will not occur.

I believe the solution is to move the dispatcher before the transitionTo statement. I've made this change on my project and it works as expected.

mgonto commented 9 years ago

Thanks for this :)