4

I am trying to make HTTP Auth Interceptor Module work together with $resource.

for now, i have a basic app working with services like this :

angular.module('myApp.services', ['ngResource']).
factory('User', function($resource){
    return $resource('path/to/json/', {}, { 'login': { method: 'GET' } });
});

and then controllers like this one :

angular.module('myApp.controllers', []).
controller('User', ['$scope', 'User', function($scope, List) 
    {    $scope.user = User.query();
]);

and the app :

angular.module('myApp', ['myApp.services', 'myApp.directives', 'myApp.controllers']).
config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/dashboard', {templateUrl: 'partials/dashboard.html', controller: 'Dashboard'});
    $routeProvider.when('/user', {templateUrl: 'partials/user.html', controller: 'TrackingCtrl'});
    $routeProvider.otherwise({redirectTo: '/dashboard'});
}]);

Until now, everything is working like expected. Then, i put an http auth on the server, so the json files http status are 401 and the browser display a login popup. I would like to use the HTTP Auth Interceptor Module to replace the browser login popup and handle the login. it is the purpose of this script, right ?

To do so, i am trying to understand the demo of the script and make that work with my app.

first, i injected 'http-auth-interceptor' to the app.

then, this was added to index.html

<body ng-controller="UserCtrl" class="auth-demo-application waiting-for-angular">

and the login form above ng-view:

<div id="login-holder">
    <div id="loginbox">
        <div id="login-inner" ng:controller="LoginController">
             <form ng-submit="submit()">
                  Username: <input type="text" class="login-inp" ng-model="username"/><br/>
                  Password: <input type="password" class="login-inp" ng-model="password"/><br/>
                  <input type="submit" class="submit-login"/>
             </form>
        </div>
    </div>
</div>

and the script at the bottom of the page:

<script src="lib/directives/http-auth-interceptor.js"></script>

in controller.js :

    controller('LoginController', ['$scope', 'authService', 'User', function ($scope, authService, User) {
    $scope.submit = function() {
        User.login( 
            function(user, response) { // success
                authService.loginConfirmed();
                console.log('login confirmed');
            }
        ); 
    }
}])

and this directive too :

directive('authDemoApplication', function() {
    return {
        restrict: 'C',
        link: function(scope, elem, attrs) {
            //once Angular is started, remove class:
            elem.removeClass('waiting-for-angular');

            var login = elem.find('#login-holder');
            var main = elem.find('#content');

            login.hide();

            scope.$on('event:auth-loginRequired', function() {
                login.slideDown('slow', function() {
                    main.hide();
                });
            });
            scope.$on('event:auth-loginConfirmed', function() {
                main.show();
                login.slideUp();
            });
        }
    }
})

Ok… that's it. but it's not working at all: the browser loggin form is still there. and that's the only way to login.

any idea on what i should do to make this work ?

4

1 回答 1

4

从他们的网页:

典型用例:

  • 某处:$http(...).then(function(response) { do-something-with-response })被调用,
  • 该请求的响应是 HTTP 401,
  • 'http-auth-interceptor' 捕获初始请求并广播 'event:auth-loginRequired',
  • 您的应用程序会拦截它以显示登录对话框(或其他任何内容),
  • 一旦您的应用程序确定身份验证正常,您将调用:authService.loginConfirmed()
  • 现在将重试您最初失败的请求,最后,将触发do-something-with-response 。

所以你唯一需要做的就是有一个父范围来监听even:auth-loginRequired。这可以在指令中完成,在控制器中,没关系。我没有使用过该软件,但我在想象这样的事情:

angular.module('myApp', [])
.service('api', function(httpAutenticationService, $rootScope, $location) {
  $rootScope.$on('event:auth-loginRequired', function() {
    // For the sake of this example, let's say we redirect the user to a login page
    $location.path('/login')
  })
  $http.get(API_URL).then( ... ); // If the user is not authenticated, this will be intercepted (1)
})
.controller('LoginController', function(httpAutenticationService) {
  // Let's imagine you have a route with the 'controller' option to this controller
  $http.get(LOGIN_URL).then(function() {
    httpAutenticationService.loginConfirmed(); // The success callback from (1) will be executed now
  }, function(){
    alert("Invalid credentials")
  })
})
于 2013-09-13T03:05:52.290 回答