mardi 22 septembre 2015

How do I display the current users details after sign in?

I have a user who lands on the /sign-in page which triggers the signIn action in the sessions controller.

This is what I suspect happens, when the user first lands on the /sign-in page:

  1. The model and afterModel hooks would have already executed. And since this.currentSession.get('isAuthenticated') is false, it never sets the model.
  2. When the user successfully signs in, and is transitioned to the articles.index, the model and afterModel hooks are not called again.
  3. The application template renders undefined for {{log model.profile.imageUrl}} and no profile image is displayed

Application route:

model() {
  if (this.currentSession.get('isAuthenticated')) {
    console.log('in application route');
    return this.store.findRecord('user', this.currentSession.get('id'));
  }
},

afterModel: function(model, transition) {
  // if (this.currentSession.get('isAuthenticated')) {
  if (Ember.isPresent(model)) {
    return model.get('profile');
  }
}

Application template:

{{log model.profile.imageUrl}}
{{image-tag imageUrl=model.profile.imageUrl size="mini" class="-small -round"}}

Sessions controller (sign-in):

export default Ember.Controller.extend({
  actions: {
    signIn(content) {
      // For teh sake of simplicity, this is supposed to exect a Ember.$.ajax({
      this.currentSession.set('token', 'abc123');
      this.currentSession.set('isAuthenticated', true);
      this.transitionToRoute('articles.index');
    }
  }
});

I can fix this by forcing a page reload after successfull sign in. In the signIn action, I can replace this.transitionToRoute('articles.index'); with document.location = '/articles';, which will trigger the model and afterModel hooks since this.currentSession.get('isAuthenticated') is now true.

However is document.location still appropriate to use today? I normally see this suggestion in Ember tutorials from a few years back.

If not, is there a different approach I can take?




Aucun commentaire:

Enregistrer un commentaire