【发布时间】:2013-09-13 00:14:36
【问题描述】:
我正在尝试使HTTP Auth Interceptor 模块与$resource 一起工作。
现在,我有一个基本的应用程序可以使用这样的服务:
angular.module('myApp.services', ['ngResource']).
factory('User', function($resource){
return $resource('path/to/json/', {}, { 'login': { method: 'GET' } });
});
然后是这样的控制器:
angular.module('myApp.controllers', []).
controller('User', ['$scope', 'User', function($scope, List)
{ $scope.user = User.query();
]);
和应用程序:
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'});
}]);
到目前为止,一切都按预期进行。然后,我在服务器上放了一个 http auth,所以 json 文件 http 状态为 401,浏览器显示登录弹出窗口。我想使用 HTTP Auth Interceptor Module 来替换浏览器登录弹出窗口并处理登录。这是这个脚本的目的,对吧?
为此,我试图了解demo of the script 并使其与我的应用程序一起使用。
首先,我将'http-auth-interceptor' 注入应用程序。
然后,这被添加到 index.html
<body ng-controller="UserCtrl" class="auth-demo-application waiting-for-angular">
和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>
以及页面底部的脚本:
<script src="lib/directives/http-auth-interceptor.js"></script>
在 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');
}
);
}
}])
还有这个指令:
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();
});
}
}
})
好的……就是这样。但它根本不起作用:浏览器登录表单仍然存在。这是登录的唯一方式。
对我应该做些什么来完成这项工作有任何想法吗?
【问题讨论】:
标签: http authentication angularjs http-authentication