2

我正在添加功能以允许用户拒绝未过期的 oauth 令牌。(我正在使用 ember-simple-auth-oauth2 和自定义 oauth2 实现)。

我想使用被拒绝的令牌通知客户他们的令牌被手动拒绝。

来自服务器的 401 响应包含令牌不再有效的原因({message: "Token was expired by ip 1.1.1.1"})。

session 或 application mixin 中的 invalidationSucceeded 回调或事件似乎都没有将 401 请求传递给此信息。

有没有办法访问在重定向之前返回 401 的请求正文?

4

3 回答 3

3

如果您使用的是401 Unauthorized 将触发该authorizationFailed操作ApplicationRouteMixin。如果你不使用ApplicationRouteMixin你可以订阅会话的authorizationFailed事件

于 2014-09-07T08:29:00.317 回答
1

您可以自定义适配器并覆盖 ajaxError 方法。以下是示例:

import DS from 'ember-data';    
export default DS.RESTAdapter.extend({
     host: url,

     ajaxError: function(jqXHR) {
         var error = this._super(jqXHR);

         if (jqXHR && jqXHR.status === 401) {
             var jsonErrors = Ember.$.parseJSON(jqXHR.responseText)["errors"];                 
             return new DS.InvalidError(jsonErrors);
         } else {
             return error;
         }
     }
});
于 2014-09-09T11:14:31.080 回答
0

What if you override the authenticate action in your controller and handle the failure in there?

authenticate: function () {
  var promise = this._super(),
    _this = this;

  promise.then(function(result) {
    // code to do if succeeded
  }).catch(function(result) {
    // code to do if failed
  });
}

You just need to make sure that you call super to hand off the actual authenticate logic.

于 2014-09-05T16:31:40.830 回答