Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate authorize method #1994

Merged
merged 5 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions addon/mixins/data-adapter-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import { isPresent } from '@ember/utils';

/**
__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
{{#crossLink "BaseAuthorizer/authorize:method"}}{{/crossLink}}) with header
name and header content arguments.
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.__
Expand All @@ -20,7 +19,14 @@ import { isPresent } from '@ember/utils';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';

export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
authorizer: 'authorizer:application'
headers: computed('session.data.authenticated.token', function() {
let headers = {};
if (this.session.isAuthenticated) {
headers['Authorization'] = `Bearer ${this.session.data.authenticated.token}`;
}

return headers;
})
});
```

Expand Down Expand Up @@ -90,6 +96,7 @@ export default Mixin.create({
`headersForRequest` *should* replace it after the resolution of the RFC.

@method ajaxOptions
@deprecated DataAdapterMixin/ajaxOptions:method
@protected
*/
ajaxOptions() {
Expand All @@ -103,6 +110,10 @@ export default Mixin.create({
xhr.setRequestHeader(headerName, headerValue);
});
} else {
deprecate('Ember Simple Auth: The authorize method should no longer be used. Instead, set the headers property or implement it as a computed property.', false, {
id: `ember-simple-auth.data-adapter-mixin.authorize`,
until: '3.0.0'
});
this.authorize(xhr);
}

Expand Down Expand Up @@ -132,9 +143,9 @@ export default Mixin.create({
@protected
*/
headersForRequest() {
deprecate('Ember Simple Auth: The headersForRequest method should no longer be used. Instead, implement the authorize method or the headers property.', false, {
deprecate('Ember Simple Auth: The headersForRequest method should no longer be used. Instead, set the headers property or implement it as a computed property.', false, {
id: `ember-simple-auth.data-adapter-mixin.headers-for-request`,
until: '2.0.0'
until: '3.0.0'
});

const authorizer = this.get('authorizer');
Expand Down
17 changes: 16 additions & 1 deletion tests/unit/mixins/data-adapter-mixin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ describe('DataAdapterMixin', () => {
expect(authorize).to.have.been.called;
});

it('shows a deprecation warning when `authorize` is called', function() {
let warnings = [];
registerDeprecationHandler((message, options, next) => {
warnings.push(message);
next(message, options);
});

adapter.authorize = () => {};
adapter.set('authorizer', null);
adapter.ajaxOptions();
hash.beforeSend();

expect(warnings[0]).to.eq('Ember Simple Auth: The authorize method should no longer be used. Instead, set the headers property or implement it as a computed property.');
});

it('preserves an existing beforeSend hook', function() {
const existingBeforeSend = sinon.spy();
hash.beforeSend = existingBeforeSend;
Expand Down Expand Up @@ -133,7 +148,7 @@ describe('DataAdapterMixin', () => {

adapter.headersForRequest();

expect(warnings[0]).to.eq('Ember Simple Auth: The headersForRequest method should no longer be used. Instead, implement the authorize method or the headers property.');
expect(warnings[0]).to.eq('Ember Simple Auth: The headersForRequest method should no longer be used. Instead, set the headers property or implement it as a computed property.');
});

it('preserves existing headers by parent adapter', function() {
Expand Down