2

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?

Alan Peabody
  • 3,507
  • 1
  • 22
  • 26

3 Answers3

3

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.

marcoow
  • 4,062
  • 1
  • 14
  • 21
  • Yeah, I have been trying those two methods. Unfortunately I need access to the response body of the 401 which does not appear to be available in either. Is it possible to get it? – Alan Peabody Sep 07 '14 at 18:05
  • The response body isn't passed to the `authorizationFailed` action, you could setup your own `Ember.$(document).ajaxError` hook though. – marcoow Sep 08 '14 at 06:41
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;
         }
     }
});
Tushar Patel
  • 365
  • 3
  • 16
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.