Skip to content

Instantly share code, notes, and snippets.

@timmyomahony
Last active January 19, 2020 18:26
Show Gist options
  • Save timmyomahony/13937c8b9ff3123e32a3bf612176a565 to your computer and use it in GitHub Desktop.
Save timmyomahony/13937c8b9ff3123e32a3bf612176a565 to your computer and use it in GitHub Desktop.
An Ember Ajax service that works with Ember Simple Auth
// see https://timmyomahony.com/blog/ember-ajax-and-ember-simple-auth/
import AjaxService from 'ember-ajax/services/ajax';
import ENV from 'connected-insight/config/environment';
import {inject as service} from '@ember/service';
import {computed} from '@ember/object';
export default AjaxService.extend({
session: service(),
host: ENV.serverHost,
authorizer: 'authorizer:token',
headers: computed('session.authToken', {
get() {
const authorizer = this.get('authorizer');
let headers = {};
this.get('session').authorize(authorizer, (headerName, headerValue) => {
headers[headerName] = headerValue;
});
return headers;
}
}),
request() {
return this._super(...arguments).catch(error => {
if (isUnauthorizedError(error)) {
this.get('session').invalidate();
}
throw error;
});
}
});
@Redsandro
Copy link

Thanks for getting back. There is magic in the AjaxService, so I guess I read up on that some more! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment