Skip to content

Commit 31a5a35

Browse files
Merge #1183
1183: Don’t use global events for login action r=carols10cents Test coverage for this stuff is not great, but I tested it manually. (I have the whole stack running locally now \o/)
2 parents 82ac341 + 9037fea commit 31a5a35

File tree

3 files changed

+57
-39
lines changed

3 files changed

+57
-39
lines changed

app/mixins/authenticated-route.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
11
import Mixin from '@ember/object/mixin';
22
import { inject as service } from '@ember/service';
3-
import $ from 'jquery';
43

54
export default Mixin.create({
65
flashMessages: service(),
76

87
beforeModel(transition) {
9-
let user = this.session.get('currentUser');
10-
if (user !== null) {
11-
return;
12-
}
13-
14-
// The current user is loaded asynchronously, so if we haven't actually
15-
// loaded the current user yet then we need to wait for it to be loaded.
16-
// Once we've done that we can retry the transition and start the whole
17-
// process over again!
18-
if (!window.currentUserDetected) {
19-
transition.abort();
20-
$(window).on('currentUserDetected', function() {
21-
$(window).off('currentUserDetected');
22-
transition.retry();
23-
});
24-
} else {
25-
this.session.set('savedTransition', transition);
8+
return this.session.checkCurrentUser(transition, () => {
269
this.get('flashMessages').queue('Please log in to proceed');
27-
return this.transitionTo('index');
28-
}
10+
});
2911
},
3012
});

app/routes/application.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
11
import Route from '@ember/routing/route';
22
import { inject as service } from '@ember/service';
3-
import $ from 'jquery';
43

54
export default Route.extend({
6-
7-
ajax: service(),
8-
95
flashMessages: service(),
106

11-
async beforeModel() {
12-
if (this.session.get('isLoggedIn') && this.session.get('currentUser') === null) {
13-
try {
14-
let response = await this.get('ajax').request('/api/v1/me');
15-
this.session.set('currentUser', this.store.push(this.store.normalize('user', response.user)));
16-
} catch(_) {
17-
this.session.logoutUser();
18-
} finally {
19-
window.currentUserDetected = true;
20-
$(window).trigger('currentUserDetected');
21-
}
22-
} else {
23-
window.currentUserDetected = true;
24-
}
7+
beforeModel() {
8+
this.session.loadUser();
259
},
2610

2711
actions: {

app/services/session.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import Service from '@ember/service';
1+
import Service, { inject as service } from '@ember/service';
22

33
export default Service.extend({
44
savedTransition: null,
5+
abortedTransition: null,
56
isLoggedIn: false,
67
currentUser: null,
8+
currentUserDetected: false,
9+
10+
ajax: service(),
11+
store: service(),
12+
router: service(),
713

814
init() {
915
this._super(...arguments);
@@ -29,6 +35,7 @@ export default Service.extend({
2935

3036
logoutUser() {
3137
this.set('savedTransition', null);
38+
this.set('abortedTransition', null);
3239
this.set('isLoggedIn', null);
3340
this.set('currentUser', null);
3441

@@ -37,5 +44,50 @@ export default Service.extend({
3744
} catch(e) {
3845
// ignore error
3946
}
47+
},
48+
49+
loadUser() {
50+
if (this.get('isLoggedIn') && !this.get('currentUser')) {
51+
this.fetchUser()
52+
.catch(() => this.logoutUser())
53+
.finally(() => {
54+
this.set('currentUserDetected', true);
55+
let transition = this.get('abortedTransition');
56+
if (transition) {
57+
transition.retry();
58+
this.set('abortedTransition', null);
59+
}
60+
});
61+
} else {
62+
this.set('currentUserDetected', true);
63+
}
64+
},
65+
66+
fetchUser() {
67+
return this.get('ajax').request('/api/v1/me')
68+
.then((response) => {
69+
this.set('currentUser', this.get('store').push(this.get('store').normalize('user', response.user)));
70+
});
71+
},
72+
73+
checkCurrentUser(transition, beforeRedirect) {
74+
if (this.get('currentUser')) {
75+
return;
76+
}
77+
78+
// The current user is loaded asynchronously, so if we haven't actually
79+
// loaded the current user yet then we need to wait for it to be loaded.
80+
// Once we've done that we can retry the transition and start the whole
81+
// process over again!
82+
if (!this.get('currentUserDetected')) {
83+
transition.abort();
84+
this.set('abortedTransition', transition);
85+
} else {
86+
this.set('savedTransition', transition);
87+
if (beforeRedirect) {
88+
beforeRedirect();
89+
}
90+
return this.get('router').transitionTo('index');
91+
}
4092
}
4193
});

0 commit comments

Comments
 (0)