Last active
February 7, 2016 01:36
-
-
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.
This file contains hidden or 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
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; | |
} | |
}); | |
This file contains hidden or 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
// 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