Search code examples
javascriptangularjsangular-ui-routerangular-http-auth

How do I apply state transition inside AngularJS $rootScope event listeners?


I am currently facing a problem in using angular-http-auth library which is handling a state transition from events of event:auth-loginRequired. I am using angular-ui-router for my routing systems.

Anyway here's some pieces of code from the library that emit the events :

case 401:
    var deferred = $q.defer();
    httpBuffer.append(config, deferred);
    $rootScope.$broadcast('event:auth-loginRequired', rejection);
    return deferred.promise;

And I was catching the events with these pieces of codes inside myApp.run

$rootScope.$on('event:auth-loginRequired', function(){
    console.log('Token Invalid / Expired');
    $state.transitionTo('login');
});

I was able to get the 'Token Invalid / Expired' inside my console. However, the state transition didn't work at all and I didn't get any errors on my console too. I've tried using $location.path('/login'), but it doesn't work too.

Can anyone please help me with the solutions? Any kind of help is really appreciated. Thanks!


Solution

  • [UPDATE - SOLVED]

    Sorry, I think I've found the solution, it's not because of the transition doesn't works inside Angular events, but I was so stupid that I myself have an event.preventDefault() handling whenever the app tried to redirect to login state (which I was trying to redirect when the app catches the events) if there's still a token inside the browser's cookies. I'm so sorry for the question.

    Once again thank you for trying to help me!