1
votes

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');
    }
  }
});
3
I'm not sure if I see how that would have been working in ember data 1.0 beta 8.Kingpin2k
@Kingpin2k It is working, but I'd be open to suggestions for how to make it better. What doesn't seem like it should work?Peter Brown
There's no blocking mechanism in place in that code that would force the user to be fetched beforehand.Kingpin2k
@Kingpin2k right, that's the whole point of this post. :)Peter Brown
Yes, but I'm saying there wasn't one when you were using beta 8 either.Kingpin2k

3 Answers

2
votes

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

```

2
votes

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...
      });
    }
  }
}
0
votes

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