mardi 16 juin 2015

Ember Simple Auth - injecting current user into every route

I am building a site using Ember Simple Auth.

I followed these instructions to try and add the current user object to the session and it worked, using this slightly adapted code:

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

export default {
  name: "current-user",
  before: "simple-auth",
  initialize: function(container) {
    Session.reopen({
      setCurrentUser: function() {
        var accessToken = this.get('secure.token');
        var _this = this;
        if (!Ember.isEmpty(accessToken)) {
          return container.lookup('store:main').find('user', 'me').then(function(user) {
            _this.set('content.currentUser', user);
          });
        }
      }.observes('secure.token'),
      setAccount: function() {
        var _this = this;
        return container.lookup('store:main').find('account', this.get('content.currentUser.account.content.id')).then(function(account) {
          _this.set('content.account', account);
        });
      }.observes('content.currentUser'),
    });
  }
};

However, using the latest version of Ember I'm getting the following:

DEPRECATION: lookup was called on a Registry. The initializer API no longer receives a container, and you should use an instanceInitializer to look up objects from the container. See http://ift.tt/1KNHHU9 for more details.

I know that I need to split the above into /app/initializers and /app/instance-initializers (as per the notes here) but I'm not quite sure how to go about it.

Of course, if there is an easier/cleaner way to make the user and account objects available to every route/template I'd love to hear them :)

Thanks




Aucun commentaire:

Enregistrer un commentaire