auth.controller.js
10.7 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
(function() {
'use strict';
angular
.module('dialoga')
.controller('AuthPageController', AuthPageController);
/** @ngInject */
function AuthPageController($scope, $rootScope, $window, $location, $state, $timeout, $interval, APP, AUTH_EVENTS, AuthService, DialogaService, Session, vcRecaptchaService, $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.APP = APP;
vm.AUTH_EVENTS = AUTH_EVENTS;
vm.AuthService = AuthService;
vm.DialogaService = DialogaService;
vm.Session = Session;
vm.vcRecaptchaService = vcRecaptchaService;
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.signup = {};
vm.terms = null;
vm.loadingTerms = null;
vm.delay = 3; // segundos
vm.countdown = 0;
vm.recaptchaResponse = null;
vm.recaptchaWidgetId = null;
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.clearMessages();
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('[register success] response', response);
});
vm.$scope.$on(vm.AUTH_EVENTS.registerFailed, function(event, response) {
vm.$log.debug('[register error] response', response);
// REMOVED: feedback alread on "reject handler"
// var reason = response.data.message;
// vm.errorMessage = reason;
});
vm.$scope.$on('oauthClientPluginResult', function(event, response) {
vm.$log.debug('response', response);
var logged_in = response.data.logged_in;
// var message = response.data.message;
var private_token = response.data.private_token;
// Garante que o 'user' sempre terá a propriedade 'private_token'
if (response.data.user && !response.data.user.private_token) {
response.data.user.private_token = private_token;
}
// Se o usuário autorizou o login via a rede social, 'logged_in' é 'true'
if (logged_in) {
var currentUser = vm.Session.create(response.data);
vm.$rootScope.currentUser = currentUser;
vm.$rootScope.$broadcast(vm.AUTH_EVENTS.loginSuccess, currentUser);
}
});
// reCaptcha Listeners
vm.setWidgetId = function(widgetId) {
// store the `widgetId` for future usage.
// For example for getting the response with
// `recaptcha.getResponse(widgetId)`.
vm.$log.info('Created widget ID:', widgetId);
vm.recaptchaWidgetId = widgetId;
};
vm.setResponse = function(response) {
// Update local captcha response
vm.$log.debug('Response available', response);
vm.recaptchaResponse = response;
};
vm.cbExpiration = function() {
// reset the 'response' object that is on scope
vm.$log.debug('cbExpiration');
};
};
AuthPageController.prototype.onClickLogout = function() {
var vm = this;
vm.AuthService.logout();
};
AuthPageController.prototype.submitSignup = function($event, credentials) {
var vm = this;
credentials.g_recaptcha_response = vm.recaptchaResponse;
vm.AuthService.register(credentials)
.then(function(/*response*/) {
// SUCCESS
vm.signupSuccess = true;
// vm._startRedirect();
})
.catch(function(response) {
// In case of a failed validation you need to reload the captcha
// because each response can be checked just once
vm.vcRecaptchaService.reload(vm.recaptchaWidgetId);
vm.signupError = true;
vm.signupErrorTitle = 'Erro!';
vm.signupErrorMessage = response.data.message;
// 4xx client error
if (response.status >= 400 && response.status < 500) {
var errors = JSON.parse(response.data.message);
if (errors && errors.email) {
vm.signupErrorMessage = 'E-mail já está em uso.';
}
}
// 5xx server error
if (response.status >= 500 && response.status < 600) {
vm.internalError = true;
}
})
.finally(function(){
// vm.loadingSubmit = false;
});
};
AuthPageController.prototype.submitSignin = function(credentials) {
var vm = this;
vm.AuthService.login(credentials)
.then(function(/*user*/) {
vm.showSigninSuccessMessage();
vm._startRedirect(); //
}, function(response) {
// handle view
vm.$log.error('Error on "submitSignin"', response);
vm.signinError = true;
// 4xx client error
if (response.status >= 400 && response.status < 500) {
vm.signinErrorTitle = 'Erro!';
vm.signinErrorContent = response.data.message;
if (response.status === 401) {
vm.signinErrorTitle = 'Acesso não autorizado!';
vm.signinErrorContent = 'E-mail ou senha incorretos.';
}
}
});
};
AuthPageController.prototype.submitRecover = function($event, recoverForm) {
var vm = this;
// init vars
vm.loadingSubmitRecover = true;
// get form data
var data = {
login: recoverForm.login.$modelValue,
g_recaptcha_response: vm.recaptchaResponse
};
var promiseRequest = vm.AuthService.forgotPassword(data);
// SUCCES
promiseRequest.then(function(response) {
vm.$log.debug('recover success.response', response);
vm.recoverSuccess = true;
});
// ERROR
promiseRequest.catch(function(response) {
vm.$log.debug('recover error.response', response);
vm.recoverError = true;
vm.recoverErrorMessage = response.data.message;
// Client Error
if (response.status >= 400 && response.status < 500) {
if (response.status === 404) {
vm.recoverErrorMessage = 'E-mail não cadastrado no Dialoga Brasil.';
}
}
// Server Error
if (response.status >= 500 && response.status < 600) {
vm.internalError = true;
}
});
// ALWAYS
promiseRequest.finally(function() {
vm.loadingSubmitRecover = false;
});
};
AuthPageController.prototype.submitConfirmationForm = function($event, confirmationForm) {
var vm = this;
// get form data
var data = {
login: confirmationForm.login.$modelValue,
g_recaptcha_response: vm.recaptchaResponse
};
// get captcha token
vm.AuthService.resendConfirmation(data)
.then(function(response) {
vm.$log.debug('resendConfirmation success.response', response);
vm.resendConfirmationSuccess = true;
// Feedback para usuário já ativo na plataforma
var user = response.data.users[0];
if (user && (user.activated === true)) {
vm.resendConfirmationSuccessTitle = 'Usuário já está ativo!';
vm.resendConfirmationSuccessMessage = 'O e-mail informado já foi confirmado.';
}else {
vm.resendConfirmationSuccessTitle = 'Pronto!';
vm.resendConfirmationSuccessMessage = 'Em instantes você receberá em seu e-mail um link para confirmar o seu cadastro.';
}
})
.catch(function(response) {
vm.$log.debug('resendConfirmation error.response', response);
vm.resendConfirmationError = true;
vm.resendConfirmationErrorMessage = response.data.message;
// Client Error
// if (response.status >= 400 && response.status < 500){}
// Server Error
if (response.status >= 500 && response.status < 600) {
vm.internalError = true;
}
})
.finally(function() {
});
};
AuthPageController.prototype.clearMessages = function() {
var vm = this;
// success
vm.signupSuccess = false;
vm.signinSuccess = false;
vm.confirmSuccess = false;
// error
vm.signinError = false;
vm.signupError = false;
};
AuthPageController.prototype.showSigninSuccessMessage = function() {
var vm = this;
vm.signinSuccess = true;
vm.successMessage = 'Login efetuado com sucesso!';
};
AuthPageController.prototype._startRedirect = function() {
var vm = this;
if (!vm.hasRedirect) {
vm.$log.debug('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);
// start redirect delay
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=' + vm.APP.facebook_app_id;
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=' + vm.APP.google_app_id;
vm.$window.oauthClientAction(url);
};
})();