0

I have classic setup of Ember-simple-auth, in ApplicationRoute I use

model: function () {
  return Ember.RSVP.hash({
    user: this.store.find('gsUser').then(function(data) {
      return data.get('content')[0]
    })
  });
},

setupController: function(controller, model) {
  this.controllerFor('user').set('content', model.user);
}

When user losts authorization, and you open the page. ApplicationRoute::model is fired first, server returns 401 and other execution is stopped.

GET http://localhost:8000/app_dev.php/api/1/users.json 401 (Unauthorized)
Error while loading route: undefined 

model should be fired only when authentication is successfull.

I saw that there is sessionAuthenticationSucceeded but I've tried all the ways to listen to it, noone worked. How to listen to this event and get data from server when user is successfully authenticated?

11/06 22:57 UPDATE:enter code here

One solution for this problem that I've managed to achieve, but it seems totally not ember way:

App.ApplicationRoute = Ember.Route.extend(Ember.SimpleAuth.ApplicationRouteMixin, {
  skipModelLoading: false,

  beforeModel: function() {
    this.set('skipModelLoading', !this.get('session').get('isAuthenticated'));
  },

  model: function () {
    if (this.get('skipModelLoading')) {
      return;
    }

    return Ember.RSVP.hash({
      user: this.store.find('gsUser').then(function(data) {
        return data.get('content')[0]
      })
    });
  },

  setupController: function(controller, model) {
    if (this.get('skipModelLoading')) {
      return;
    }

    this.controllerFor('user').set('content', model.user);
  }
});
Ruslan Zavacky
  • 363
  • 2
  • 7
  • Do you check that the user is authenticated in the route's `beforeModel` either manually or by mixing in the `AuthenticatedRouteMixin` (the latter might be a problem because it would apply to the login route as well then)? – marcoow Jun 11 '14 at 16:11
  • @marcoow: Tried, I can stop transition: beforeModel: function(transition) { if (!this.get('session').get('isAuthenticated')) { transition.abort(); } } But what to do next? ) Transition to login route works bad, as it is infinite redirect. How it should be done in the right way? – Ruslan Zavacky Jun 11 '14 at 19:50
  • Yes, that's the problem - you cannot require the session to be authenticated in the application route's `model` method as that would apply to all routes including the login route. – marcoow Jun 11 '14 at 20:09
  • @marcoow any ideas how to receive User model from server after successfull login & when page might be refreshed? ) – Ruslan Zavacky Jun 11 '14 at 20:11

2 Answers2

1

I assume you're loading the authenticated user in that model method. I'd do it differently and attach that property to the session as shown in this example: https://github.com/simplabs/ember-simple-auth/blob/master/examples/4-authenticated-account.html#L101

marcoow
  • 4,062
  • 1
  • 14
  • 21
  • do you have some open-sourced examples that use your library (except those two provided somewhere in repository)? I really struggle to implement it right. For me it seems like I want really simple task to do, I want to listen for success authorization event, but now its like second day of debugging ember.js + ember simple auth to achieve it.. and still no luck ( – Ruslan Zavacky Jun 11 '14 at 20:40
  • There's 9 examples or is included in the repository plus some external ones linked in the README. – marcoow Jun 12 '14 at 05:35
1

I think I found a more ember-way solution for my problem:

App.ApplicationRoute = Ember.Route.extend(Ember.SimpleAuth.ApplicationRouteMixin, {
  onSessionIsAuthenticated: function () {
    var isAuthenticated = this.get('session').get('isAuthenticated');

    if (!isAuthenticated) {
      return false;
    }

    var userController = this.controllerFor('user');

    return Ember.RSVP.hash({
      user: this.store.find('gsUser').then(function (data) {
        userController.set('content', data.get('content')[0]);
      })
    });
  }.observes('session.isAuthenticated').on('init')
});
Ruslan Zavacky
  • 363
  • 2
  • 7