Ember Simple Auth - authorizationFailed未执行会话失效或transitionTo

时间:2015-02-12 14:51:08

标签: session ember.js authorization ember-simple-auth

问题:未经授权的请求不会使会话无效并转换为根网址。默认方法不起作用,所以我重写它。

我的自定义授权商

var CustomAuthorizer = Base.extend({
 authorize: function(jqXHR, requestOptions) {
    if (this.get('session.isAuthenticated') && !Ember.isEmpty(this.get('session.token'))) {
        jqXHR.setRequestHeader('ApiKey', this.get('session.token'));
    }
 }
});

authorizationFailed hook

authorizationFailed: function(session){
  console.log('authorizationFailed'); //displays in console
  this.get('session').invalidate('authenticator:custom', {}); //doesn't invalidate
  console.log(this.get('session')); //still return authenticated session
  this.transitionTo('index'); //doesn't transition
}

我知道问题是因为失效是异步的,因此失效的承诺不会及时返回,以便console.log吐出无效的会话。还有另一种方法吗?

1 个答案:

答案 0 :(得分:0)

invalidate返回一个承诺,您应该等待转换前履行承诺。

this.get('session').invalidate('authenticator:custom', {}).then(function() {
  this.transitionTo('index');
});