import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';

Deprecated:Call the session service's invalidate method in the adapter's handleResponse method instead in case of a 401 response

This mixin can be used to make Ember Data adapters authorize all outgoing API requests by injecting a header. The adapter's headers property can be set using data read from the session service that is injected by this mixin.

The DataAdapterMixin will also invalidate the session whenever it receives a 401 response for an API request.

// app/adapters/application.js
          import JSONAPIAdapter from '@ember-data/adapter/json-api';
          import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
          import { computed } from '@ember/object';
          
          export default class ApplicationAdapter extends JSONAPIAdapter.extend(DataAdapterMixin) {
            @computed('session.data.authenticated.token')
            get headers() {
              let headers = {};
              if (this.session.isAuthenticated) {
                headers['Authorization'] = Bearer ${this.session.data.authenticated.token};
              }
          
              return headers;
            }
          }
          

The DataAdapterMixin requires Ember Data 1.13 or later.

Methods

source

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

source protected

handleResponse(status, headers, payload, requestData)

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

Properties

source public readonly

session: SessionService

The session service.

Extends

Ember.Mixin

Properties