I am trying to make HTTP Auth Interceptor The module works with $ resource.
I have a basic application that works with these services:
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 application:
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'}); }]);
So far, everything is working as expected. Then I set HTTP authentication on the server, so the status of the http-json files is 401, and the browser displays a login popup. I would like to use the HTTP Auth Interceptor Module to replace the browser login popup and handle the login. this is the purpose of this script, right?
To do this, I am trying to understand a demo script and do this with my application.
First, I injected 'http-auth-interceptor'
into the application.
then it 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 script at the bottom of the page:
<script src="lib/directives/http-auth-interceptor.js"></script>
in controller.js file:
controller('LoginController', ['$scope', 'authService', 'User', function ($scope, authService, User) { $scope.submit = function() { User.login( function(user, response) {
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(); }); } } })
It's good. but it doesn't work at all: the browser log form still exists. and that the only way to log in.
Any idea on what I should do to make this work?