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; | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for getting back. There is magic in the
AjaxService
, so I guess I read up on that some more! 👍