samedi 26 septembre 2015

Ember delay controller until authentication completes

I have a situation where I need to get some properties on the session, but I haven't found a successful solution to delay the loading of the route after a successful login.

So here's the deal - On the initial login, the this.get('session.currentUser') is not set. When I go to a different route and then come back, it is set correctly. If I'm on the phone-numbers route after login and then refresh the page, the phone-numbers load correctly because of the deferReadiness and advanceReadiness in the initializer. I can't deferReadiness before a login because the app is already loaded and ready.

The only part that's missing is that after the user logs in, it should load the numbers in the routes/phone-numbers.js, the last block of code pasted below. However, the myStoreId is not loaded because the session.currentUser is not set yet.

I've tried so many things to try and get this and am looking for some ideas. It's so close to working, but just missing one little piece.

// initializers/current-user.js
import Ember from 'ember';
import Session from 'simple-auth/session';

export default {
  name: 'current-user',
  before: 'simple-auth',

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

        // don't know how to check if the app is already ready
        try{
          application.deferReadiness();
          console.log('deferred');
        }catch(e){}

        if(this.get('isAuthenticated')) {
          console.log('running the isAuthenticated obs');

          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(function(data) {
              _this.set('myStore', data.get('firstObject'));
              application.advanceReadiness();
            });
          })
        }
      }.observes('isAuthenticated')
    });
  }
};

// controllers/application.js
export default Ember.Controller.extend({
  myStore: Ember.computed(function(){
    // return the store object that is associated with the current user
    if(this.get('session.isAuthenticated')){
      if(this.get('session.myStore')){
        return this.get('session.myStore');
      }else{
        console.log(this.get('session.currentUser'));
        // this is where the problem is. The session.currentUser is not populated yet.

        this.get('store').find('store', {user: this.get('session.currentUser.id')}).then(function(data) {
          this.get('session').set('myStore', data.get('firstObject'));
          return this.get('session.myStore');
        });
      }
    }
  }),
});


// routes/phone-numbers.js
export default Ember.Route.extend({
  setupController: function(controller, model){
    this._super(controller, model);
    let myStoreId = this.controllerFor('application').get('myStore.id');

    if(!myStoreId){
      console.log(this.get('session.currentUser'));
      // there is no currentUser set on the session after login
    }else{
      this.store.find('store-phone-number', {'store': myStoreId}).then(function(numbers){
        controller.set('numbers', numbers);
      });
    }
  },
});




Aucun commentaire:

Enregistrer un commentaire