Skip to content

Instantly share code, notes, and snippets.

@davidpdrsn
Created May 27, 2019 12:49
Show Gist options
  • Save davidpdrsn/f74824dd6b5b4c0018b6ea5994eafec5 to your computer and use it in GitHub Desktop.
Save davidpdrsn/f74824dd6b5b4c0018b6ea5994eafec5 to your computer and use it in GitHub Desktop.
(() => {
let Store = {
set:(key, object) => {
var json = JSON.stringify(object);
localStorage.setItem(key, json);
},
get: (key) => {
var json = localStorage.getItem(key);
return JSON.parse(json);
}
};
let override = (object, methodName, callback) => {
object[methodName] = callback(object[methodName])
}
let getTokenFromUser = () => {
let savedToken = Store.get("token");
if (savedToken) {
return savedToken;
} else {
let token = prompt("What is your bearer token?");
Store.set("token", token);
return token;
}
};
window.clearToken = () => {
Store.set("token", null);
getTokenFromUser();
};
override(window, "fetch", (original) => {
return (url, params) => {
let token = getTokenFromUser();
params["headers"]["X-GraphiQL-Authorization"] = `Bearer ${token}`;
return original.apply(this, [url, params]);
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment