繁体   English   中英

灰烬简单身份验证ApplicationRoute模型功能

[英]Ember-simple-auth ApplicationRoute model function

我有Ember-simple-auth的经典设置,在我使用的ApplicationRoute中

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);
}

当用户失去授权时,您将打开页面。 首先触发ApplicationRoute :: model,服务器返回401,其他执行停止。

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

仅在身份验证成功时才触发model

我看到有sessionAuthenticationSucceeded但是我已经尝试了所有方法来收听它,没有人工作。 成功验证用户身份后,如何侦听此事件并从服务器获取数据?

11/06 22:57更新: enter code here

我设法解决的这个问题的一种解决方案, 但似乎完全不是余烬方式

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);
  }
});

我假设您正在使用该model方法加载经过身份验证的用户。 我将以不同的方式进行操作,并将该属性附加到会话,如以下示例所示: https : //github.com/simplabs/ember-simple-auth/blob/master/examples/4-authenticated-account.html#L101

我认为我为我的问题找到了更多解决方案:

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')
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM