【问题标题】:Ember-simple-auth ApplicationRoute model functionEmber-simple-auth ApplicationRoute 模型函数
【发布时间】:2014-06-11 13:03:56
【问题描述】:

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

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

【问题讨论】:

  • 您是否检查了用户是否在路由的beforeModel 中手动或通过混合AuthenticatedRouteMixin 进行身份验证(后者可能是一个问题,因为它也适用于登录路由)?
  • @marcoow: 试过了,我可以停止转换: beforeModel: function(transition) { if (!this.get('session').get('isAuthenticated')) { transition.abort() ; } } 但是接下来要做什么呢? ) 过渡到登录路由效果不好,因为它是无限重定向。应该如何以正确的方式完成?
  • 是的,这就是问题所在 - 您不能要求在应用程序路由的 model 方法中对会话进行身份验证,因为这将适用于包括登录路由在内的所有路由。
  • @marcoow 任何想法如何在成功登录后从服务器接收用户模型以及何时刷新页面? )

标签: ember.js ember-simple-auth


【解决方案1】:

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

【讨论】:

  • 您是否有一些使用您的库的开源示例(除了存储库中提供的那两个)?我真的很难正确实施它。对我来说,似乎我想做一个非常简单的任务,我想听成功授权事件,但现在它就像调试 ember.js + ember 简单身份验证的第二天来实现它.. 仍然没有运气(
  • 存储库中有 9 个示例或包含在自述文件中链接的一些外部示例。
【解决方案2】:

我想我为我的问题找到了一个更有效的解决方案:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多