Capturing failed request with ember-simple-auth

元气小坏坏 提交于 2019-12-24 01:01:46

问题


I am adding functionality to allow users to reject un-expired oauth tokens. (I am using ember-simple-auth-oauth2 and a custom oauth2 implimentation).

I would like to notify clients using a rejected token that their token was manually rejected.

The 401 response from the server contains the reason the token is no longer valid ({message: "Token was expired by ip 1.1.1.1"}).

None of the invalidationSucceeded callbacks or events in the session or application mixin seem to have the 401 request passed to this info.

Is there a way to access the body of the request that returned the 401 before the redirect?


回答1:


You can customize the adapter and override the ajaxError method. Following is the example:

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



回答2:


401 Unauthorized will trigger the authorizationFailed action if you're using the ApplicationRouteMixin. If you're not using the ApplicationRouteMixin you can subscribe to the session's authorizationFailed event.




回答3:


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.



来源:https://stackoverflow.com/questions/25689415/capturing-failed-request-with-ember-simple-auth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!