1

So I have a simple ESA setup with a custom authenticator/authorizer wrapping Basic HTTP Auth. The login route uses UnauthenticatedRouteMixin and the login controller is basically just authenticating against the custom authenticator, and afterwards fetches the user object from a REST service:

export default Ember.Controller.extend({
  session: Ember.inject.service('session'),

  actions: {
    login() {

      var self = this;

      let { userName, password } = this.getProperties('userName', 'password');
      this.get('session').authenticate('authenticator:basicauth', userName, password).then(function ()
        {

            self.store.queryRecord('user', { loginname : userName }).then( function (user) {

              console.log(user.get('id'));

              self.get('session').set('data.filterUserId', user.get('id'));

            });

        }).catch((reason) => {
            this.set('errorMessage', reason.error || reason);
        });

    }
  }

});

Now, in the first authenticated route (via AuthenticatedRouteMixin), for testing I also simply console.log the stored session property:

import Ember from 'ember';
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';

export default Ember.Route.extend(AuthenticatedRouteMixin, {

    session: Ember.inject.service('session'),

    model: function () {    

        console.log(this.get('session.data.filterUserId'));

    },

});

In the console output, I observe that the model hook seems to be executed before the authenticator's promise is resolved, i.e. first I get "null", then the ID.

How can I make the AuthenticatedRoute wait for the actual login authentication to resolve?

Julian Rubisch
  • 547
  • 7
  • 21

1 Answers1

1

Ember Simple Auth will transition to the routeAfterAuthentication as soon as authentication succeeds which is right when the promise returned by the session service's authenticate method resolves. You're only loading the user asynchronously after that though so that it is only loaded after the transition as been done already.

For an example of how to load data when the session becomes authenticated see the application route in the dummy app: https://github.com/simplabs/ember-simple-auth/blob/master/tests/dummy/app/routes/application.js

You must be aware that the session can become authenticated when the user logs in in the current tab/window but also when the user logs in in another tab or window (where the sessionAuthenticated event could basically be triggered at any time then) - that's why you need to load all data you need when the session is authenticated whenever the sessionAuthenticated method is called on the application route (of course you can handle the session service's authenticated event yourself instead of using the ApplicationRouteMixin) and not when the promise returned by the session service's authenticate method resolves.

marcoow
  • 4,062
  • 1
  • 14
  • 21
  • thanks, it's becoming much clearer now. I suspect my confusion was with the `sessionAuthenticated` method (and its relation to the `authenticationSucceeded` event) - maybe a short example in the readme would help... – Julian Rubisch Feb 12 '16 at 11:25
  • sorry for the late follow-up - I reworked my app to follow your suggestions, however I had to move the setting of the `filterUser` property to the `loadCurrentUser()` method to make it work – Julian Rubisch Mar 01 '16 at 10:38