Skip to content

Instantly share code, notes, and snippets.

@deivinsontejeda
Last active February 7, 2016 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deivinsontejeda/72b68592e49ef3fe1b1d to your computer and use it in GitHub Desktop.
Save deivinsontejeda/72b68592e49ef3fe1b1d to your computer and use it in GitHub Desktop.
Tiny service which use ember-simple-auth 1.0.0 and wrap any remote call.
import Ember from 'ember';
/**
* This service wrap any remote call which needs authorization
* in order to set necessary headers to being authorized
* once the user is logged in
*
*/
const { inject } = Ember;
export default Ember.Service.extend({
session: inject.service(),
ajaxOptions() {
const { token, data2 } = this.get('session.data.authenticated');
return {
type: 'GET',
dataType: 'json',
beforeSend: (xhr) => {
xhr.setRequestHeader('Authorization', `Bearer ${token}`);
xhr.setRequestHeader('X-Header-2', data2);
}
};
},
/**
* This is a private API
* @param {Object} data support by http://api.jquery.com/jQuery.ajax/
*/
perform(options) {
let promise;
this.get('session').authorize('authorizer:application', () => {
promise = Ember.$.ajax(Object.assign(this.ajaxOptions(), options));
});
return promise;
}
});
// once the request service is injected
return this.get('request').perform({ url: url, data: params })
.then((response) => { return response; } )
.fail((err) => { console.debug(JSON.parse(err.responseText).message); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment