Auth0 example

This commit is contained in:
Maurits van der Schee 2018-10-09 13:59:23 +02:00
commit 9efa4cbf4a

View file

@ -0,0 +1,36 @@
<html>
<head>
<meta charset="utf-8" />
<script>
var domain = ''; // hostname ending in '.auth0.com'
var clientId = ''; // client id as defined in auth0
var audience = ''; // api audience as defined in auth0
window.onload = function () {
var match = RegExp('[#&]access_token=([^&]*)').exec(window.location.hash);
var accessToken = match && decodeURIComponent(match[1].replace(/\+/g, ' '));
document.location.hash = '';
if (accessToken) {
url = '/api.php/records/posts?join=categories&join=tags&join=comments&filter=id,eq,1';
var req = new XMLHttpRequest();
req.onreadystatechange = function () {
if (req.readyState==4) {
console.log(req.responseText);
jsonObject = JSON.parse(req.responseText);
document.getElementById('output').innerHTML = JSON.stringify(jsonObject, undefined, 4);
}
}
req.open("GET", url, true);
req.setRequestHeader('X-Authorization', 'Bearer '+accessToken);
req.send();
} else {
redirectUri = document.location.href;
url = 'https://'+domain+'/authorize?audience='+audience+'&response_type=token&client_id='+clientId+'&redirect_uri='+redirectUri;
document.location = url;
}
};
</script>
</head>
<body>
<pre id="output"></pre>
</body>
</html>