0

I'm using Ember Simple auth, and I'm using an initializer to set the current authenticated user as a property on my session:

Session.reopen({
  setCurrentUser: function() {
    var id = this.get("user_id");
    var self = this;

    if (!Ember.isEmpty(id)) {
      return container.lookup("store:main").find("user", id).then(function(user) {
        self.set("currentUser", user);
      });
    }
  }.observes("user_id")
});

Is it possible to force the router to wait until currentUser has been set before ending the initial app transition? I want certain components to be able to access session.currentUser as soon as the app boots up.

Sam Selikoff
  • 12,366
  • 13
  • 58
  • 104

1 Answers1

0

I had solved a similar problem, and I concluded to set 'user' field for 'AuthController' (Session-like object) as a promise. Then, in all places where it needs I call that field and wait until the promise is fulfilled. Like this:

App.SomeRoute = Em.Route.extend({
    beforeModel: function(transition) {
        var auth = this.controllerFor('auth');

        if (!auth.get('isLogged')) {
            transition.abort();
            auth.set('abortedTransition', transition);

            auth.get('user')
                .then(function() {
                    transition.retry();

                    auth.set('abortedTransition', null);
                })
                .fail(function() {
                // transit to index or something like that
                });
        }

        return true;
      }
})

Basically, it turns down current transition, if user isn't logged into our system, and retries this transition right after user becomes authenticated.

I'm sure you can adapt this approach to your situation.

Microfed
  • 2,832
  • 22
  • 25