Skip to content

Commit 4aa2e93

Browse files
committed
login auth request keeps spinning while making redirect
1 parent 9867593 commit 4aa2e93

File tree

4 files changed

+65
-54
lines changed

4 files changed

+65
-54
lines changed

client/xhr.js

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,27 @@ function xhr(options) {
4747
body = JSON.stringify(body);
4848
}
4949

50-
51-
request.addEventListener('loadstart', event => {
52-
request.timeStart = Date.now();
53-
var e = wrapEvent('xhrstart', event);
54-
document.dispatchEvent(e);
55-
});
56-
request.addEventListener('loadend', event => {
57-
var e = wrapEvent('xhrend', event);
58-
document.dispatchEvent(e);
59-
});
60-
request.addEventListener('success', event => {
61-
var e = wrapEvent('xhrsuccess', event);
62-
e.result = event.result;
63-
document.dispatchEvent(e);
64-
});
65-
request.addEventListener('fail', event => {
66-
var e = wrapEvent('xhrfail', event);
67-
e.reason = event.reason;
68-
document.dispatchEvent(e);
69-
});
50+
if (!options.noDocumentEvents) {
51+
request.addEventListener('loadstart', event => {
52+
request.timeStart = Date.now();
53+
var e = wrapEvent('xhrstart', event);
54+
document.dispatchEvent(e);
55+
});
56+
request.addEventListener('loadend', event => {
57+
var e = wrapEvent('xhrend', event);
58+
document.dispatchEvent(e);
59+
});
60+
request.addEventListener('success', event => {
61+
var e = wrapEvent('xhrsuccess', event);
62+
e.result = event.result;
63+
document.dispatchEvent(e);
64+
});
65+
request.addEventListener('fail', event => {
66+
var e = wrapEvent('xhrfail', event);
67+
e.reason = event.reason;
68+
document.dispatchEvent(e);
69+
});
70+
}
7071

7172
if (!options.raw) { // means we want json
7273
request.setRequestHeader("Accept", "application/json");
@@ -143,15 +144,6 @@ function xhr(options) {
143144

144145
}
145146

146-
function addUrlParam(url, name, value) {
147-
var param = encodeURIComponent(name) + '=' + encodeURIComponent(value);
148-
if (~url.indexOf('?')) {
149-
return url + '&' + param;
150-
} else {
151-
return url + '?' + param;
152-
}
153-
154-
}
155147

156148
document.addEventListener('xhrfail', function(event) {
157149
new notification.Error(event.reason);

handlers/auth/client/authModal.js

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ function AuthModal(options) {
2828
}
2929

3030
var self = this;
31-
if (!options.callback) {
32-
options.callback = function() {
33-
self.successRedirect();
34-
};
35-
}
3631

3732
this.options = options;
3833
this.setContent(clientRender(loginForm));
@@ -168,7 +163,7 @@ AuthModal.prototype.initEventHandlers = function() {
168163
var request = this.request({
169164
method: 'POST',
170165
url: '/auth/reverify',
171-
body: payload
166+
body: payload
172167
});
173168

174169
var self = this;
@@ -214,10 +209,10 @@ AuthModal.prototype.submitRegisterForm = function(form) {
214209
payload.append("successRedirect", this.options.successRedirect);
215210

216211
var request = this.request({
217-
method: 'POST',
218-
url: '/auth/register',
212+
method: 'POST',
213+
url: '/auth/register',
219214
normalStatuses: [201, 400],
220-
body: payload
215+
body: payload
221216
});
222217

223218
var self = this;
@@ -226,15 +221,14 @@ AuthModal.prototype.submitRegisterForm = function(form) {
226221
if (this.status == 201) {
227222
self.setContent(clientRender(loginForm));
228223
self.showFormMessage(
229-
"<p>С адреса [email protected] отправлено письмо со ссылкой-подтверждением.</p>" +
230-
"<p><a href='#' data-action-verify-email='" + form.elements.email.value + "'>перезапросить подтверждение.</a></p>",
224+
"<p>С адреса [email protected] отправлено письмо со ссылкой-подтверждением.</p>" +
225+
"<p><a href='#' data-action-verify-email='" + form.elements.email.value + "'>перезапросить подтверждение.</a></p>",
231226
'success'
232227
);
233228
return;
234229
}
235230

236231
if (this.status == 400) {
237-
debugger;
238232
for (var field in event.result.errors) {
239233
self.showInputError(form.elements[field], event.result.errors[field]);
240234
}
@@ -263,10 +257,10 @@ AuthModal.prototype.submitForgotForm = function(form) {
263257
payload.append("successRedirect", this.options.successRedirect);
264258

265259
var request = this.request({
266-
method: 'POST',
267-
url: '/auth/forgot',
260+
method: 'POST',
261+
url: '/auth/forgot',
268262
normalStatuses: [200, 404],
269-
body: payload
263+
body: payload
270264
});
271265

272266
var self = this;
@@ -325,22 +319,36 @@ AuthModal.prototype.submitLoginForm = function(form) {
325319

326320
if (hasErrors) return;
327321

328-
var request = this.request({
329-
method: 'POST',
330-
url: '/auth/login/local',
322+
var request = xhr({
323+
method: 'POST',
324+
url: '/auth/login/local',
325+
noDocumentEvents: true, // we handle all events/errors in this code
331326
normalStatuses: [200, 401],
332-
body: new FormData(form)
327+
body: new FormData(form)
333328
});
334329

335-
var self = this;
336-
request.addEventListener('success', function(event) {
330+
var onEnd = this.startRequestIndication();
331+
332+
request.addEventListener('success', (event) => {
337333

338-
if (this.status != 200) {
339-
self.onAuthFailure(event.result.message);
334+
if (request.status == 401) {
335+
onEnd();
336+
this.onAuthFailure(event.result.message);
340337
return;
341338
}
342339

343-
self.onAuthSuccess(event.result.user);
340+
// don't stop progress indication if login successful && we're making redirect
341+
if (!this.options.callback) {
342+
this.onAuthSuccess(event.result.user);
343+
} else {
344+
onEnd();
345+
this.onAuthSuccess(event.result.user);
346+
}
347+
});
348+
349+
request.addEventListener('fail', (event) => {
350+
onEnd();
351+
this.onAuthFailure(event.reason);
344352
});
345353

346354
};
@@ -362,7 +370,11 @@ AuthModal.prototype.openAuthPopup = function(url) {
362370
*/
363371
AuthModal.prototype.onAuthSuccess = function(user) {
364372
window.currentUser = user;
365-
this.options.callback();
373+
if (this.options.callback) {
374+
this.options.callback();
375+
} else {
376+
this.successRedirect();
377+
}
366378
};
367379

368380

handlers/auth/strategies/localStrategy.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ module.exports = new LocalStrategy({
1818

1919
co(function*() {
2020

21+
// @tyv UNCOMMENT SPINNER
22+
// yield function(callback) {};
23+
2124
if (!email) throw new UserAuthError('Укажите email.');
2225
if (!password) throw new UserAuthError('Укажите пароль.');
2326

pm2/post_deploy.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#!/bin/bash
22

3+
# Hard upgrade:
34
# stop
45
# migrations
56
# start
67

8+
# Regular upgrade (this script):
9+
# reload
710

11+
# this causes several seconds downtime, uncomment when the upgrade is required
812
#/usr/local/bin/pm2 updatePM2
913

1014
# fixme: switch to startOrGracefulReload with 2 processes, with 1 process it doesn't actually restart the process

0 commit comments

Comments
 (0)