open-nomad/ui/app/services/token.js

33 lines
681 B
JavaScript
Raw Normal View History

2017-09-19 14:47:10 +00:00
import Ember from 'ember';
import fetch from 'fetch';
2017-09-19 14:47:10 +00:00
const { Service, computed, assign } = Ember;
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;
},
}),
authorizedRequest(url, options = {}) {
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
});