-
Notifications
You must be signed in to change notification settings - Fork 6
/
controllers.js
94 lines (80 loc) · 2.48 KB
/
controllers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* Controllers */
function MyCtrl1() {}
MyCtrl1.$inject = [];
function MyCtrl2() {
}
MyCtrl2.$inject = [];
var myApp = angular.module('myApp').controller({
LoginController: function ($scope, $http, authService) {
$scope.submit = function() {
alert('LoginController inside 3');
//debugger;
$http.post(
'http://localhost:8001/api/user/login/',
JSON.stringify({ username: $scope.username, password: $scope.password })
).success(
function(data) {
alert('LoginController submit success');
//debugger;
$.cookie('username', data.username, { expires: 7 });
$.cookie('key', data.key, { expires: 7 });
$http.defaults.headers.common['Authorization'] = 'ApiKey ' +
data.username + ':' + data.key;
authService.loginConfirmed();
}
).error(
function(data) {
alert('LoginController submit error');
$scope.errorMsg = data.reason;
//debugger;
}
);
};
}
}).controller({
ContentController: function ($scope, $http) {
$scope.publicContent = [];
$scope.restrictedContent = [];
$scope.publicAction = function() {
$http.post(
'http://localhost:8001/api/myproperty/paymenttype/',
JSON.stringify({ name: $scope.publicData })
).success(
function(response) {
$scope.publicContent.push(response);
}
).error(
function(response) {
alert('error 99');
}
);
};
$scope.restrictedAction = function() {
alert('restrictedAction');
$http.post(
'http://localhost:8001/api/myproperty/paymenttype/',
JSON.stringify({ name: $scope.restrictedData }), {
//transformResponse: function(data),
timeout: 5000
}
).success(
function(response) {
alert('restrictedAction inside');
// this piece of code will not be executed until user is authenticated
$scope.restrictedContent.push(response);
}
);
};
$scope.logout = function() {
$http.post('http://localhost:8001/auth/logout/').success(function() {
$scope.restrictedContent = [];
$.cookie('key', null);
$http.defaults.headers.common['Authorization'] = null;
}).error(function() {
// This should happen after the .post call either way.
$.cookie('key', null);
$http.defaults.headers.common['Authorization'] = null;
});
};
}
});