Skip to content

Commit 898edd7

Browse files
committed
Don't synchronously wait for /me
There's no need to wait for it on page load as we should get straight to loading the content and worry about our user account later!
1 parent 9b6b9ef commit 898edd7

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

app/mixins/authenticated-route.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,19 @@ import Ember from 'ember';
33
export default Ember.Mixin.create({
44
beforeModel: function(transition) {
55
var user = this.session.get('currentUser');
6-
if (user === null) {
6+
if (user !== null) { return; }
7+
8+
// The current user is loaded asynchronously, so if we haven't actually
9+
// loaded the current user yet then we need to wait for it to be loaded.
10+
// Once we've done that we can retry the transition and start the whole
11+
// process over again!
12+
if (!window.currentUserDetected) {
13+
transition.abort();
14+
Ember.$(window).on('currentUserDetected', function() {
15+
Ember.$(window).off('currentUserDetected');
16+
transition.retry();
17+
});
18+
} else {
719
this.session.set('savedTransition', transition);
820
this.controllerFor('application').set('nextFlashError',
921
'Please log in to proceed');

app/routes/application.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ export default Ember.Route.extend({
77
if (this.session.get('isLoggedIn') &&
88
this.session.get('currentUser') === null)
99
{
10-
return ajax('/me').then(function(response) {
10+
ajax('/me').then(function(response) {
1111
var user = self.store.push('user', response.user);
1212
user.set('api_token', response.api_token);
1313
self.session.set('currentUser', user);
1414
}).catch(function() {
1515
self.session.logoutUser();
16+
}).finally(function() {
17+
window.currentUserDetected = true;
18+
Ember.$(window).trigger('currentUserDetected');
1619
});
1720
}
1821
},

0 commit comments

Comments
 (0)