Search code examples
ember.jsember-dataember-simple-auth

Ember-data 1.0.0.beta.9 upgrade changes load order of simple auth session data


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');
    }
  }
});

Solution

  • 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');
        }
      }
    });