Search code examples
ember.jsember-simple-auth

Capturing failed request with ember-simple-auth


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?


Solution

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