vendredi 18 septembre 2015

ember set user object based on authenticated user

I have a coffee ordering application and am trying to connect a user to their store.

I'm authenticating the user and then setting that user on the session which is based on ember-simple-auth. That part works fine setting a session.currentUser property, but then I want to set a session.myStore object as well, but I'm running into issues.

Right now I'm doing all this in an initializer:

// initializers/custom-user.js
export default {
  name: 'current-user',
  before: 'simple-auth',

  initialize: function(container, application) {
    Session.reopen({
      setCurrentUser: function() {
        let appController = container.lookup("controller:application");

        application.deferReadiness();

        if(this.get('isAuthenticated')) {
          let store = container.lookup('store:main');
          let _this = this;

          return store.find('user', 'me').then((user) => {
            // set the current user to be used on the session object
            this.set('currentUser', user);
          }).then(function(){
            // set the store for the current user
            store.find('store', {'user': _this.get('currentUser.id')}).then((data) => {
              console.log(data);
              _this.set('myStore', data);
              application.advanceReadiness();
            });
          })
        }
      }.observes('isAuthenticated')
    });
  }
};

I am getting data back where I have console.log(data); but I don't think it's the correct object. It's returning the class and not the object. In other routes/controllers I want to be able to return something like this.get('session.myStore.id') but the session.myStore does not have the data that I'm looking for




Aucun commentaire:

Enregistrer un commentaire