2017-12-15 21:39:18 +00:00
|
|
|
import Service from '@ember/service';
|
|
|
|
import { computed } from '@ember/object';
|
|
|
|
import { assign } from '@ember/polyfills';
|
2017-11-14 18:50:29 +00:00
|
|
|
import fetch from 'nomad-ui/utils/fetch';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
export default Service.extend({
|
|
|
|
secret: computed({
|
|
|
|
get() {
|
|
|
|
return window.sessionStorage.nomadTokenSecret;
|
|
|
|
},
|
|
|
|
set(key, value) {
|
|
|
|
if (value == null) {
|
|
|
|
window.sessionStorage.removeItem('nomadTokenSecret');
|
|
|
|
} else {
|
|
|
|
window.sessionStorage.nomadTokenSecret = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
}),
|
2017-10-07 00:14:08 +00:00
|
|
|
|
2018-01-08 20:09:29 +00:00
|
|
|
authorizedRequest(url, options = { credentials: 'include' }) {
|
2017-10-07 00:14:08 +00:00
|
|
|
const headers = {};
|
|
|
|
const token = this.get('secret');
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
headers['X-Nomad-Token'] = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fetch(url, assign(options, { headers }));
|
|
|
|
},
|
2017-09-19 14:47:10 +00:00
|
|
|
});
|