client/app/auth/auth_controller.js
changeset 60 7d624c1013a3
child 66 86f880fe5a25
equal deleted inserted replaced
59:df402f454856 60:7d624c1013a3
       
     1 (function(){
       
     2 	'use strict';
       
     3 
       
     4 	angular.module('ammicoAuth',['ngRoute'])
       
     5 	.controller('authCtrl', function($rootScope, $routeParams, $modal){
       
     6 		$modal.open({
       
     7 			templateUrl: 'auth/' + $routeParams.action + '_modal.html',
       
     8 			controller: 'auth' + $routeParams.action + 'Ctrl'
       
     9 		});
       
    10 	})
       
    11 	.controller('authloginCtrl', function($modalInstance, $scope, authApi, $window, $http, $location, $rootScope){
       
    12 		$scope.login = function(){
       
    13 			authApi.login.save({username:$scope.username, password:$scope.password}, function(data){
       
    14 				$rootScope.globals.userLogged = true;
       
    15 				$window.sessionStorage.token = data.token;
       
    16 				$http.defaults.headers.common.Authorization = 'Token ' + $window.sessionStorage.token;
       
    17 				$modalInstance.close();
       
    18 				$location.path('/books');
       
    19 			});
       
    20 		};
       
    21 		$scope.cancel = function () {
       
    22 			$modalInstance.dismiss('cancel');
       
    23 			$location.path( '/' );
       
    24 		};
       
    25 	})
       
    26 	.controller('authlogoutCtrl', function($modalInstance, $scope, $window, $http, $location, $rootScope){
       
    27 		delete $window.sessionStorage.token;
       
    28 		$rootScope.globals.userLogged = false;
       
    29 		delete $http.defaults.headers.common.Authorization;
       
    30 		
       
    31 		$scope.cancel = function () {
       
    32 			$modalInstance.close();
       
    33 			$location.path( '/' );
       
    34 		};
       
    35 	})
       
    36 	.controller('authregisterCtrl', function($scope, $modalInstance, $location){
       
    37 		$scope.register = function () {
       
    38 		};
       
    39 		$scope.back = function () {
       
    40 			$modalInstance.close();
       
    41 			$location.path( '/auth/login' );
       
    42 		};
       
    43 	});
       
    44 })();