Search code examples
ember.jsember-simple-auth

Why must I lookup store on my simple-auth custom session instead of injecting it as a service?


I have a initializer like this:

import Ember from 'ember';
import Session from 'simple-auth/session';

var SessionWithCurrentUser = Session.extend({
  store: Ember.inject.service(),
  currentUser: function() {
    console.log(this.get('store'));
    console.log(this.store);
    console.log(this.container.lookup('service:store'));
  }.property('secure.access_token')
});

export default {
  name: 'custom-session',
  after: 'ember-data',
  initialize(registry) {
    registry.register('session:withCurrentUser', SessionWithCurrentUser);
  }
};

currentUser gets called on user interaction, long after my app has finished loading. Only the last container lookup gives the store, the other 2 is an object:

{
 _lastData: Object,
 key: "ember_simple_auth:session"
 [..]
}

What's going on? Why can't I inject the store?


Solution

  • It's because store in the current version of simple-auth is being overridden by an instance-initializer with the session storage. The next major version of simple-auth will turn the session storage into a service and we'll be able to do:

    import Ember from 'ember';
    
    const { service } = Ember.inject;
    
    export default Ember.Service.extend({
      session: service('session'),
      store: Ember.inject.service(),
    
      account: Ember.computed('session.content.secure.account_id', function() {
        const accountId = this.get('session.content.secure.account_id');
        if (!Ember.isEmpty(accountId)) {
          return DS.PromiseObject.create({
            promise: this.get('store').find('account', accountId)
          });
        }
      })
    });
    

    From the dummy app, once https://github.com/simplabs/ember-simple-auth/pull/602 is merged.