Skip to content

Commit 8bda3e2

Browse files
committed
Improve auth0 vanilla example a bit
1 parent 5f2c523 commit 8bda3e2

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

examples/clients/auth0/vanilla.html

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<html>
22
<head>
3-
<meta charset="utf-8" />
3+
<meta charset="utf-8" />
44
<script>
55
var authUrl = 'https://php-crud-api.auth0.com/authorize'; // url of auth0 '/authorize' end-point
66
var clientId = ''; // client id as defined in auth0
7-
var audience = ''; // api audience as defined in auth0
8-
window.onload = function () {
7+
var audience = 'https://your-php-crud-api/api.php'; // api audience as defined in auth0
8+
var url = '/api.php/records/posts?join=categories&join=tags&join=comments&filter=id,eq,1';
9+
10+
function requestApi() {
911
var match = RegExp('[#&]access_token=([^&]*)').exec(window.location.hash);
1012
var accessToken = match && decodeURIComponent(match[1].replace(/\+/g, ' '));
1113
if (!accessToken) {
@@ -16,18 +18,32 @@
1618
req.onreadystatechange = function () {
1719
if (req.readyState==4) {
1820
console.log(req.responseText);
19-
document.getElementById('output').innerHTML = JSON.stringify(JSON.parse(req.responseText), undefined, 4);
21+
try {
22+
document.getElementById('output').innerHTML = JSON.stringify(JSON.parse(req.responseText), undefined, 4);
23+
} catch (error) {
24+
document.getElementById('output').innerHTML = req.responseText;
25+
}
2026
}
2127
}
22-
url = '/api.php/records/posts?join=categories&join=tags&join=comments&filter=id,eq,1';
2328
req.open("GET", url, true);
2429
req.setRequestHeader('X-Authorization', 'Bearer '+accessToken);
2530
req.send();
2631
}
2732
};
33+
34+
window.onload = function() {
35+
requestAPI()
36+
document.getElementById('request-btn').onclick = function(e) {
37+
e.preventDefault()
38+
requestAPI()
39+
}
40+
}
2841
</script>
2942
</head>
3043
<body>
44+
<p>
45+
<button type="button" id="request-btn">Request API</button>
46+
</p>
3147
<pre id="output"></pre>
3248
</body>
3349
</html>

0 commit comments

Comments
 (0)