Last active
January 19, 2020 18:26
-
-
Save timmyomahony/13937c8b9ff3123e32a3bf612176a565 to your computer and use it in GitHub Desktop.
An Ember Ajax service that works with Ember Simple Auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}); | |
} | |
}); |
Same question, where does AuthenticatedAjaxMixin
come from @timmyomahony?
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.
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
Hi! How is defined AuthenticatedAjaxMixin ?