Skip to content

Instantly share code, notes, and snippets.

@s-c-p
Created November 18, 2017 05:24
Show Gist options
  • Save s-c-p/649afac3d7e3c5a8a1ba7049a8fa6372 to your computer and use it in GitHub Desktop.
Save s-c-p/649afac3d7e3c5a8a1ba7049a8fa6372 to your computer and use it in GitHub Desktop.
localStorage.jwt = 'header.payload.signature';
let auth_fetch = function(api_url, token) {
let myHeader = new Headers();
myHeader.append('Content-Type', 'json/application');
myHeader.append('Authorization', 'Bearer ' + token);
let request = new Request(api_url,
{ method: 'GET'
, headers: myHeader
, mode: 'no-cors'
});
fetch(request)
.then(function (response) {
if (!response.ok) {
throw response.statusText
}
return response.json();
})
.then(function (jsondata) {
// ensure that downloaded 'data' is available for later use
data = jsondata;
state2view(jsondata);
})
.catch(err => console.log(err));
};
auth_fetch('/api/example', localStorage.jwt);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment