I am using Ember: 2.11.0, ember-simple-auth: 1.2.0
I use the ember-simple-auth to authenticate my application via oauth2 to my REST API.
The standard behaviour of ember-simple-auth is to invalidate the users session if the server responds with a 401 status code. I want to handle this different and try to override this:
import DS from 'ember-data';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
export default DS.RESTAdapter.extend(DataAdapterMixin, {
host: 'http://localhost:3000',
authorizer: 'authorizer:oauth2',
/*
* The DataAdapterMixin invalidetes the session automatically if the server
* returns the status 401. We don't want this behaviour, so we override
* the handleResponse method.
*/
handleResponse(status) {
console.log(status);
return this._super(...arguments);
}
});
In my RestAdapter I Use the DataAdapterMixin which triggers the invalidation within the handleResponse method. So I tried to override this method in my adapter. My method is called but after my method finished, the mixins method is called by ember, as you can see here:
The Comments for the Ember superWrapper method state, that it is made to handle calls to methods of the super class and redirect them to it, but somehow it seems to redirect it to the mixin.
I have no idea why this happens. This could probably be fixed by editing the DataAdapterMixin directly, but think that wouldn't be a good idea in terms of compatibility with future versions of ember-simple-auth
I would really appreciate if someone could point me into the right direction to make the override work.
When you extend adapter from mixin, this._super(...arguments);
will call method of the mixin (if it has such method). That's why your override doesn't work. You have following options:
handleResponse
code from DS.RESTAdapter
(start from here). No this._super
call - no influence from mixin. This can be not so easy as it sounds and may be not compatible with future versions of ember dataDataAdapterMixin
by copying code from ember-simple-auth and removing/modifying it's handleResponse
method. This can be not compatible with future versions of ember-simpe-auth.Modify arguments
before calling this._super(...arguments)
, so status will be 400 instead of 401:
handleResponse: function (status) {
/**
* Modify status
*/
if (status === 401) {
status = 400;
}
/**
* Replace status in arguments.
*/
var args = Array.prototype.slice.call(arguments, 0);
args.splice(0, 1, status);
/**
* Call parent's method
*/
return this._super(...args);
}
This method is compatible with future versions - even if new argument will be added (at the moment arguments are status
, headers
, payload
), this code will work. It will stop to work if status will not be the first argument anymore (I don't think this will happen in near future).
But I want also to say that something is probably not right with your backend: 401 means "unathorized" and ember-simple-auth does what should be done in this case - invalidates session. If you need special status for some cases, I'd suggest to use 418 (I'm a teapot).