auth.controller.js
5.98 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
(function() {
'use strict';
angular
.module('dialoga')
.controller('AuthPageController', AuthPageController);
/** @ngInject */
function AuthPageController($scope, $rootScope, $window, $location, $state, $timeout, $interval, AUTH_EVENTS, AuthService, DialogaService, Session, $log) {
var vm = this;
vm.$scope = $scope;
vm.$rootScope = $rootScope;
vm.$window = $window;
vm.$location = $location;
vm.$state = $state;
vm.$timeout = $timeout;
vm.$interval = $interval;
vm.AUTH_EVENTS = AUTH_EVENTS;
vm.AuthService = AuthService;
vm.DialogaService = DialogaService;
vm.Session = Session;
vm.$log = $log;
vm.init();
vm.loadData();
vm.attachListeners();
vm.$rootScope.focusMainContent();
vm.$log.debug('AuthPageController');
}
AuthPageController.prototype.init = function() {
var vm = this;
// init variables
vm.signin = {};
vm.singup = {};
vm.terms = null;
vm.loadingTerms = null;
vm.delay = 3; // segundos
vm.countdown = 0;
vm.search = vm.$location.search();
var redirect = vm.search.redirect_uri || '';
if (redirect && redirect.length > 0) {
vm.params = JSON.parse('{"' + decodeURI(redirect).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}');
vm.hasRedirect = true;
}
// attach events
vm.currentUser = vm.Session.getCurrentUser();
// handle login
vm.$scope.$on(vm.AUTH_EVENTS.loginSuccess, function() {
vm.currentUser = vm.Session.getCurrentUser();
});
// handle logout
vm.$scope.$on(vm.AUTH_EVENTS.logoutSuccess, function() {
vm.currentUser = vm.Session.getCurrentUser();
});
};
AuthPageController.prototype.loadData = function() {
var vm = this;
// load terms
vm.loadingTerms = true;
vm.DialogaService.getTerms(function(data) {
vm.loadingTerms = false;
vm.terms = data.article;
}, function(error) {
// vm.$log.debug('handleSuccess.error', error);
vm.loadingTerms = false;
vm.error = error;
});
};
AuthPageController.prototype.attachListeners = function() {
var vm = this;
vm.$scope.$on(vm.AUTH_EVENTS.registerSuccess, function(event, response) {
vm.$log.debug('TODO: handle register success');
vm.$log.debug('[register success] response', response);
});
vm.$scope.$on(vm.AUTH_EVENTS.registerFailed, function(event, response) {
vm.$log.debug('TODO: handle register error');
vm.$log.debug('[register error] response', response);
var reason = response.data.message;
vm.errorMessage = reason;
});
vm.$scope.$on('oauthClientPluginResult', function(event, response) {
vm.$log.debug('response', response);
// var logged_id = response.data.logged_id;
// var private_token = response.data.private_token;
// var user = response.data.user;
});
var stop = null;
stop = vm.$interval(function(){
var $el = angular.element('#serpro_captcha');
if ($el && $el.length > 0 ){
vm.$window.initCaptcha($el[0]);
vm.$interval.cancel(stop);
stop = undefined;
}
}, 200);
};
AuthPageController.prototype.onClickLogout = function() {
var vm = this;
vm.AuthService.logout();
};
AuthPageController.prototype.submitSingup = function(credentials) {
var vm = this;
vm.AuthService.register(credentials).then(function(response) {
vm.$log.debug('register success.response', response);
// TODO: mensagens de sucesso
// 'Cadastro efetuado com sucesso.'
// 'Verifique seu email para confirmar o cadastro.'
vm.successMessage = '<h3>Cadastro efetuado com sucesso.</h3>' + '<p>Verifique seu <b>email</b> para confirmar o cadastro.</p>';
vm.redirectBack();
}, function(response) {
vm.$log.debug('register error.response', response);
var message = response.data.message;
vm.errorMessage = message;
if(response.data.code === 500){
vm.internalError = true;
}
// TODO: mensagens de erro
// TODO: tratar multiplos erros
// javascript_console_message: "Unable to reach Serpro's Captcha validation service"
// message: "Internal captcha validation error"
});
};
AuthPageController.prototype.submitSignin = function(credentials) {
var vm = this;
vm.AuthService.login(credentials).then(function(user) {
// handle view
vm.$log.debug('user', user);
vm.successMessage = 'Login efetuado com sucesso!';
vm.redirectBack();
}, function() {
// handle view
});
};
AuthPageController.prototype.redirectBack = function() {
var vm = this;
if (!vm.hasRedirect) {
vm.$log.warn('No redirect params defined.');
return;
}
// start countdown
vm.countdown = vm.delay;
var stop = null;
stop = vm.$interval(function() {
vm.countdown--;
if (vm.countdown <= 0) {
vm.$interval.cancel(stop);
stop = undefined;
}
}, 1000);
vm.$timeout(function() {
var state = vm.params.state;
switch (state){
case 'inicio':
vm.$state.go(state, {
event_id: vm.params.event_id,
task: vm.params.task
});
break;
case 'programa':
vm.$state.go(state, {
slug: vm.params.slug,
task: vm.params.task,
proposal_id: vm.params.proposal_id,
});
break;
default:
vm.$log.debug('State not handled yet:', state);
break;
}
}, vm.delay * 1000);
};
AuthPageController.prototype.authWithFacebook = function() {
var vm = this;
var url = 'http://login.dialoga.gov.br/plugin/oauth_client/facebook?oauth_client_popup=true&id=1';
vm.$window.oauthClientAction(url);
};
AuthPageController.prototype.authWithGooglePlus = function() {
var vm = this;
var url = 'http://login.dialoga.gov.br/plugin/oauth_client/google_oauth2?oauth_client_popup=true&id=4';
vm.$window.oauthClientAction(url);
};
})();