Reputation: 51717
With ember-data 1.0.0-beta.8, ember-simple-auth loads the current user from my API before making any other requests. This guarantees the current user data will be available in other routes. After upgrading to ember-data 1.0.0-beta.9, it loads the current user after making other API requests.
Is there a way to force ember-simple-auth to load the current user before other data using ember-data 1.0.0-beta.9?
My custom simple auth session that looks like this:
import SimpleAuthSession from 'simple-auth/session';
export default SimpleAuthSession.extend({
updateCurrentUser: function() {
var self = this;
var userId = this.get('user_id');
if (!Ember.isEmpty(userId)) {
self.set('currentUser', self.container.lookup('store:main').find('current-user', userId));
}
}.observes('user_id')
});
My authentication initializer:
import MyAppSession from 'my-app-ember/lib/my-app-session';
import FacebookAuthenticator from 'my-app-ember/lib/facebook-authenticator';
export default {
name: 'authentication',
before: 'simple-auth',
initialize: function(container) {
container.register('session:my-app-session', MyAppSession);
container.register('simple-auth-authenticator:facebook', FacebookAuthenticator);
window.ENV = window.ENV || {};
window.ENV['simple-auth'] = {
session: 'session:my-app-session',
authorizer: 'simple-auth-authorizer:oauth2-bearer',
routeAfterAuthentication: 'moments'
};
window.ENV['simple-auth-oauth2'] = {
serverTokenEndpoint: MyAppEmberENV.API_NAMESPACE + '/oauth/token'
};
}
};
Here's an example of a place where I'm depending on the currentUser object to be set on the session in an afterModel hook and is broken after the upgrade:
export default Ember.Route.extend({
model: function() {
return this.store.find('moment');
},
afterModel: function() {
if (!this.get('session.currentUser.isReturningUser')) {
this.transitionTo('settings');
}
}
});
Upvotes: 1
Views: 150
Reputation: 4062
If you need to make sure that the user loaded successfully before any other API requests are made you'd have to add a custom initializer that defers readiness and only advances readiness after the user has successfully loaded:
// app/initializers/session-user.js
export default {
name: 'session-user',
after: 'simple-auth',
initialize: function(container, application) {
var session = container.lookup('simple-auth-session:main');
if (session.get('isAuthenticated')) {
application.deferReadiness();
session.get('currentUser').then(function() {
application.advanceReadiness();
}, function() {
//handle error...
});
}
}
}
Upvotes: 2
Reputation: 51717
After trying every possible combination I could think of, the only thing that worked for me was to get the current user from the session in the application route. This seems pretty hacky, but I might experiment more later to see if there's something else going on.
app/routes/application.js:
import Ember from 'ember';
import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';
export default Ember.Route.extend(ApplicationRouteMixin, {
model: function() {
if (this.get('session.user_id')) {
return this.get('session.currentUser');
}
}
});
Upvotes: 0
Reputation: 121
I'm doing something similar and don't have this issue with beta 9. I'd maybe try doing this logic in an initializer so you can run it before simple-auth init.
import Ember from 'ember';
import Session from 'simple-auth/session';
import Authorizer from 'myapp/authorizers/custom';
export default {
name: 'simple-auth-config',
before: 'simple-auth',
initialize: function(container) {
container.register('authorizer:custom', Authorizer);
window.ENV = window.ENV || {};
window.ENV['simple-auth'] = {
authorizer: 'authorizer:custom',
crossOriginWhitelist: [MyappENV.API_HOST]
};
window.ENV['simple-auth-oauth2'] = {
serverTokenEndpoint: MyappENV.API_HOST + '/token'
};
Session.reopen({
currentUser: function() {
var userId = this.get('user_id');
if (!Ember.isEmpty(userId)) {
return container.lookup('store:main').find('user', userId);
}
}.property('user_id')
});
}
}
```
Upvotes: 2