Skip to content

Instantly share code, notes, and snippets.

@ada-lovecraft
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ada-lovecraft/9414111 to your computer and use it in GitHub Desktop.
Save ada-lovecraft/9414111 to your computer and use it in GitHub Desktop.
$modal decorator to close all modals on $rootScope noauth message
torpedoApp.config(function($provide) {
$provide.decorator('$modal', function($delegate,$rootScope) {
var openFn = $delegate.open;
$delegate.open = function() {
console.debug('open!');
var regged = false;
var args = [].slice.call(arguments);
var modalInstance = openFn.apply(null, args);
modalInstance.opened.then(function(status) {
console.debug('open status:', status);
if(!!status && !regged) {
regged = true;
console.debug('registering for no auth');
var noAuthDereg = $rootScope.$on('noauth', function() {
console.debug('noauth!', modalInstance);
noAuthDereg();
modalInstance.dismiss('noauth');
});
}
});
return modalInstance;
};
return $delegate;
});
});
// and then inside the http interceptor:
if(rejection.status === 401) {
// if the user is no longer authorized, push them to the login page
console.debug('No longer authorized. Transitioning to login');
$rootScope.$broadcast('noauth');
$injector.get('$state').transitionTo('noauth.login');
}
return $q.reject(rejection);
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment