client/app/auth/auth_controller.js
author rougeronj
Tue, 07 Apr 2015 11:56:43 +0200
changeset 73 1aa813be49ec
parent 66 86f880fe5a25
child 100 537d330ad7f0
permissions -rw-r--r--
redirect to login page if the account is successfully created
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
60
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
     1
(function(){
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
     2
	'use strict';
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
     3
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
     4
	angular.module('ammicoAuth',['ngRoute'])
66
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
     5
	.controller('authCtrl', function($location, $rootScope, $routeParams, $modal){
60
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
     6
		$modal.open({
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
     7
			templateUrl: 'auth/' + $routeParams.action + '_modal.html',
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
     8
			controller: 'auth' + $routeParams.action + 'Ctrl'
66
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
     9
		}).result.then(function (result) {
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    10
			$location.path(result);
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    11
		}, function () {
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    12
			$location.path('/');
60
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    13
		});
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    14
	})
66
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    15
	.controller('authloginCtrl', function($modalInstance, $scope, authApi, $window, $http, $rootScope){
60
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    16
		$scope.login = function(){
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    17
			authApi.login.save({username:$scope.username, password:$scope.password}, function(data){
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    18
				$rootScope.globals.userLogged = true;
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    19
				$window.sessionStorage.token = data.token;
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    20
				$http.defaults.headers.common.Authorization = 'Token ' + $window.sessionStorage.token;
66
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    21
				$modalInstance.close('/books');
60
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    22
			});
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    23
		};
66
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    24
		$scope.register = function () {
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    25
			$modalInstance.close('/auth/register');
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    26
		};
60
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    27
		$scope.cancel = function () {
66
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    28
			$modalInstance.close('/');
60
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    29
		};
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    30
	})
66
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    31
	.controller('authlogoutCtrl', function($modalInstance, $scope, $window, $http, $rootScope){
60
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    32
		delete $window.sessionStorage.token;
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    33
		$rootScope.globals.userLogged = false;
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    34
		delete $http.defaults.headers.common.Authorization;
66
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    35
60
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    36
		$scope.cancel = function () {
66
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    37
			$modalInstance.close('/');
60
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    38
		};
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    39
	})
66
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    40
	.controller('authregisterCtrl', function($scope, $modalInstance, authApi){
60
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    41
		$scope.register = function () {
66
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    42
			authApi.user.save({
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    43
				username: $scope.username,
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    44
				email: $scope.email,
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    45
				password: $scope.password,
73
1aa813be49ec redirect to login page if the account is successfully created
rougeronj
parents: 66
diff changeset
    46
			}, function(){
1aa813be49ec redirect to login page if the account is successfully created
rougeronj
parents: 66
diff changeset
    47
				$modalInstance.close('/auth/login');
66
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    48
			});
60
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    49
		};
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    50
		$scope.back = function () {
66
86f880fe5a25 minor update on authcontroller : a modal closed himself sending the route to follow after
rougeronj
parents: 60
diff changeset
    51
			$modalInstance.close('/auth/login');
60
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    52
		};
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    53
	});
7d624c1013a3 add and repair auth module (it was not detected by mercurial anymore due to a cass problem on title)
rougeronj
parents:
diff changeset
    54
})();