Inject session for vanilla ajax requests

This commit is contained in:
Matthew Ryan Dillon 2015-10-20 16:09:51 -07:00
parent bbcef5459e
commit f06fbd0305
5 changed files with 17 additions and 5 deletions

View file

@ -2,6 +2,8 @@ import Ember from 'ember';
import ajaxRequest from '../../../../utils/ajax-request'; import ajaxRequest from '../../../../utils/ajax-request';
export default Ember.Route.extend({ export default Ember.Route.extend({
session: Ember.inject.service('session'),
queryParams: { queryParams: {
strain_ids: { strain_ids: {
refreshModel: true, refreshModel: true,
@ -33,7 +35,7 @@ export default Ember.Route.extend({
method: 'GET', method: 'GET',
data: params, data: params,
}; };
return ajaxRequest(url, options); return ajaxRequest(url, options, this.get('session'));
}, },
setupController: function(controller, model) { setupController: function(controller, model) {

View file

@ -2,6 +2,7 @@ import Ember from 'ember';
import ajaxRequest from '../../../../utils/ajax-request'; import ajaxRequest from '../../../../utils/ajax-request';
export default Ember.Controller.extend({ export default Ember.Controller.extend({
session: Ember.inject.service('session'),
currentUser: Ember.inject.service('session-account'), currentUser: Ember.inject.service('session-account'),
passwordConfirm: null, passwordConfirm: null,
@ -22,7 +23,7 @@ export default Ember.Controller.extend({
password: this.get('password'), password: this.get('password'),
}, },
}; };
ajaxRequest(url, options); ajaxRequest(url, options, this.get('session'));
this.transitionTo('protected.users.index'); this.transitionTo('protected.users.index');
this.get('flashMessages').information('Your password has been changed.'); this.get('flashMessages').information('Your password has been changed.');
}, },

View file

@ -2,6 +2,8 @@ import Ember from 'ember';
import ajaxRequest from '../../../../utils/ajax-request'; import ajaxRequest from '../../../../utils/ajax-request';
export default Ember.Route.extend({ export default Ember.Route.extend({
session: Ember.inject.service('session'),
apiURL: function() { apiURL: function() {
return this.get('globals.apiURL'); return this.get('globals.apiURL');
}.property(), }.property(),
@ -12,7 +14,7 @@ export default Ember.Route.extend({
model: function(params) { model: function(params) {
let url = `${this.get('apiURL')}/api/${this.get('genus')}/users/verify/${params.nonce}`; let url = `${this.get('apiURL')}/api/${this.get('genus')}/users/verify/${params.nonce}`;
return ajaxRequest(url); return ajaxRequest(url, {}, this.get('session'));
}, },

View file

@ -2,6 +2,8 @@ import Ember from 'ember';
import ajaxRequest from '../../../utils/ajax-request'; import ajaxRequest from '../../../utils/ajax-request';
export default Ember.Controller.extend({ export default Ember.Controller.extend({
session: Ember.inject.service('session'),
actions: { actions: {
save: function() { save: function() {
let url = `${this.get('globals.apiURL')}/api/${this.get('globals.genus')}/users/lockout`; let url = `${this.get('globals.apiURL')}/api/${this.get('globals.genus')}/users/lockout`;
@ -9,7 +11,7 @@ export default Ember.Controller.extend({
method: 'POST', method: 'POST',
data: { email: this.get('email') }, data: { email: this.get('email') },
}; };
ajaxRequest(url, options); ajaxRequest(url, options, this.get('session'));
this.transitionTo('login'); this.transitionTo('login');
this.get('flashMessages').information('Please check your email'); this.get('flashMessages').information('Please check your email');
}, },

View file

@ -1,9 +1,14 @@
import Ember from 'ember'; import Ember from 'ember';
export default function ajaxRequest(url, options) { export default function ajaxRequest(url, options, session) {
return new Ember.RSVP.Promise(function(resolve, reject) { return new Ember.RSVP.Promise(function(resolve, reject) {
options = options || {}; options = options || {};
options.url = url; options.url = url;
session.authorize('authorizer:application', (headerName, headerValue) => {
let authHeader = {};
authHeader[headerName] = headerValue;
options.headers = authHeader;
});
options.success = function(data) { options.success = function(data) {
resolve(data); resolve(data);
}; };