DataAdapterMixin
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
This mixin can be used to make Ember Data adapters authorize all outgoing API requests by injecting a header. It works with all authorizers that call the authorization callback (see authorize) with header name and header content arguments.
The DataAdapterMixin
will also invalidate the session whenever it
receives a 401 response for an API request.
// app/adapters/application.js
import DS from 'ember-data';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
authorizer: 'authorizer:application'
});
The DataAdapterMixin
requires Ember Data 1.13 or later.
Methods
Defines a beforeSend
hook (see http://api.jquery.com/jQuery.ajax/) that
injects a request header containing the authorization data as constructed
by the authorizer
(see
authorize). The
specific header name and contents depend on the actual authorizer that is
used.
Until emberjs/rfcs#171
gets resolved and ds-improved-ajax
feature flag
is enabled, this method will be called for every ember-data version.
headersForRequest
should replace it after the resolution of the RFC.
ensureResponseAuthorized
(status, headers, payload, requestData
)
The default implementation for handleResponse. If the response has a 401 status code it invalidates the session (see invalidate).
Override this method if you want custom invalidation logic for incoming responses.
Arguments
-
status
:Number
-
The response status as received from the API
-
headers
:Object
-
HTTP headers as received from the API
-
payload
:Any
-
The response body as received from the API
-
requestData
:Object
-
the original request information
This method is called for every response that the adapter receives from the API. If the response has a 401 status code it invalidates the session (see invalidate).
Arguments
-
status
:Number
-
The response status as received from the API
-
headers
:Object
-
HTTP headers as received from the API
-
payload
:Any
-
The response body as received from the API
-
requestData
:Object
-
the original request information
Adds request headers containing the authorization data as constructed by the authorizer.
Until emberjs/rfcs#171
gets resolved and ds-improved-ajax
feature flag
is enabled, this method will not be used.
See ajaxOptions
instead.
Properties
Default: null
The authorizer that is used to authorize API requests. The authorizer has to call the authorization callback (see authorize) with header name and header content arguments. This property must be overridden in adapters using this mixin.
When used with ember-fetch
the authorize
method will not be called and
the headers
computed property must be used instead, e.g.:
export default DS.JSONAPIAdapter.extend(AdapterFetch, DataAdapterMixin, {
headers: computed('session.data.authenticated.token', function() {
const headers = {};
if (this.session.isAuthenticated) {
headers['Authorization'] = Bearer ${this.session.data.authenticated.token}
;
}
return headers;
}),
});