Created
May 27, 2019 12:49
-
-
Save davidpdrsn/f74824dd6b5b4c0018b6ea5994eafec5 to your computer and use it in GitHub Desktop.
This file contains 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
(() => { | |
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