Skip to content

Instantly share code, notes, and snippets.

@timmyomahony
Last active January 19, 2020 18:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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;
});
}
});
@cbou
Copy link

cbou commented Mar 14, 2019

Hi! How is defined AuthenticatedAjaxMixin ?

@Redsandro
Copy link

Same question, where does AuthenticatedAjaxMixin come from @timmyomahony?

@timmyomahony
Copy link
Author

timmyomahony commented Jan 19, 2020

Sorry, it's been a while since I've written this. I've attached where my AuthenticatedAjaxMixin came from. This is over 2 years old though now so not sure if it works.

I think that including AuthenticatedAjaxMixin was just a mistake when copying over from the app I made the example from (in other words this Gist is the AuthenticatedAjaxMixin) . I'll have to review this and the blog post in a bit more detail, but I think it can just be removed.

@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