auth0 / angular-jwt

Library to help you work with JWTs on AngularJS
MIT License
0 stars 1 forks source link

isAuthenticated false after token is checked for in the same cycle. #165

Closed sourjya closed 4 years ago

sourjya commented 6 years ago

Hi, I'm using angular-jwt in conjunction with ui-router with CakePHP backend. Here are the steps I'm following:

  1. User data is posted to backend via ajax.
  2. Token is generated and sent back.
  3. Token is stored in localstorage.
  4. Login page attempts to redirect to an inner page (dashboard) that requires authentication.
  5. This is where issues are cropping up. I have this block of code in the .run() section of angular that uses the transition onBefore event and checks for authentication status using authManager.isAuthenticated().
                  $transitions.onBefore( {}, function( transition ) {

                      // Requires login?
                      // Check if destination state requires Auth.
                      var requiresLogin = transition.targetState().$state().data &&
                                          transition.targetState().$state().data.requiresLogin ?
                                          true : false;

                      console.log( 'target state' )
                      console.log( transition.targetState().$state() );

                      console.log( 'isAuthed?' );
                      console.log( authManager.isAuthenticated() );

                      // Check if destination state requires Auth.
                      if( requiresLogin ) {
                          if( authManager.isAuthenticated() !== true ) {
                              $rootScope.returnToStateName = transition.to().name;
                              $rootScope.returnToStateURL = transition.to().url;
                              return transition.router.stateService.target( 'gateway.login' );
                          }
                      }
                      else {
                          $rootScope.returnToStateName = null;
                          $rootScope.returnToStateURL = null;
                      }
                  } );

However, during the same cycle where token is fetched and written to localstorage and the state attempts to transition to the inner page, authManager.isAuthenticated() is returning null and not true and hence the state isn't transitioning.

How can I get around this?

Thank you.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you have not received a response for our team (apologies for the delay) and this is still a blocker, please reply with additional information or just a ping. Thank you for your contribution! 🙇‍♂️