【发布时间】: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