0

I'm playing around with the code of http://ngmodules.org/modules/http-auth-interceptor and I'm wondering why

$rootScope.$broadcast('loginRequired');

doesn't trigger the alert in the controller

$scope.$on('loginRequired',function() {
    alert('loginRequired');   
});

The code:

<!doctype html>
<html ng-app="myModule">
<head>
    <meta charset="utf-8">
</head>

<body>
    <div id="content" class="ng-view"></div>
    <script src="http://code.angularjs.org/1.1.5/angular.min.js"></script>
    <script>
        var buffer = angular.module('http-auth-interceptor-buffer', []);
        buffer.factory('httpBuffer',  function($injector) {
            var buffer = [];
            var $http;

            function retryHttpRequest(config, deferred) {
                function successCallback(response) {
                    deferred.resolve(response);
                }
                function errorCallback(response) {
                    deferred.reject(response);
                }
                $http = $http || $injector.get('$http');
                $http(config).then(successCallback, errorCallback);
            }

            return {

                append: function(config, deferred) {
                    buffer.push({
                        config: config,
                        deferred: deferred
                    });
                },

                retryAll: function(updater) {
                    for (var i = 0; i < buffer.length; ++i) {
                        retryHttpRequest(updater(buffer[i].config), buffer[i].deferred);
                    }
                    buffer = [];
                }
            };
        });

        var app = angular.module('myModule', ['http-auth-interceptor-buffer']);
        app.config(function($httpProvider,$routeProvider, $locationProvider) {
            $httpProvider.interceptors.push('securityInterceptor');
            $routeProvider.
            when('/one',{
                controller: 'OneCtrl',
                /*resolve: {
                    my: function(Data) {
                        return Data.getData();
                    }
                },*/
                templateUrl: './_one.html'
            }).
            when('/two', {
                controller: 'TwoCtrl',
                templateUrl:'./_two.html'
            })
            .otherwise({
                redirectTo: '/one'
            });
        });

        app.controller('OneCtrl',function($scope,Data) {
            $scope.my = Data.getData();
            $scope.$on('loginRequired',function() {
                alert('loginRequired');   
            });
        });

        app.controller('TwoCtrl',function($scope) {

        });

        app.factory('Data', function($http,$q) {
            return {
                getData : function() {
                    var deferred = $q.defer();
                    var promise = $http.get('./security.php').success(function (response) {
                        deferred.resolve(response);
                    });

                    // Return the promise to the controller
                    return deferred.promise; 
                }
            }
        });

        app.factory('securityInterceptor', function($q, $rootScope,httpBuffer) {
            return {
                request: function(config) {
                    return config || $q.when(config);
                },

                requestError: function(rejection) {
                },

                response: function(response) {
                    return response || $q.when(response);
                },

                responseError: function(rejection) {
                    if(rejection.status === 401) {
                        var deferred = $q.defer();
                        httpBuffer.append(rejection.config, deferred);
                        $rootScope.$broadcast('loginRequired');
                        return deferred.promise;
                    }

                    return $q.reject(rejection);
                }
            };
        });
    </script>
</body>
</html>

UPDATE

security.php

<?php
header('HTTP/1.1 401 Unauthorized');
$data = 'MyTets';

echo json_encode($data);

What's the trouble ?

2
  • Did you check if the event is spread before the setting of the listner? Commented Oct 30, 2013 at 16:10
  • In OneCtrl calling Data.getData(); get me from the server a 401 status I updated my post Commented Oct 30, 2013 at 16:31

1 Answer 1

0

I tested your code by putting

$rootScope.$broadcast('loginRequired');

in securityInterceptor

response: function(response) {
    $rootScope.$broadcast('loginRequired');
    return response || $q.when(response);
},

had problem to simulate the response of the php file. Your code and the events are working well. It should come from your php file which didn't return 401 right?

here is a plunker here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.