Browse Source

Improve auth0 vanilla example a bit

Sébastien JEAN 5 years ago
parent
commit
8bda3e2c17
1 changed files with 21 additions and 5 deletions
  1. 21
    5
      examples/clients/auth0/vanilla.html

+ 21
- 5
examples/clients/auth0/vanilla.html View File

@@ -1,11 +1,13 @@
1 1
 <html>
2 2
 <head>
3
-<meta charset="utf-8" /> 
3
+<meta charset="utf-8" />
4 4
 <script>
5 5
 var authUrl = 'https://php-crud-api.auth0.com/authorize'; // url of auth0 '/authorize' end-point
6 6
 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() {
9 11
     var match = RegExp('[#&]access_token=([^&]*)').exec(window.location.hash);
10 12
     var accessToken = match && decodeURIComponent(match[1].replace(/\+/g, ' '));
11 13
     if (!accessToken) {
@@ -16,18 +18,32 @@ window.onload = function () {
16 18
         req.onreadystatechange = function () {
17 19
             if (req.readyState==4) {
18 20
                 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
+                }
20 26
             }
21 27
         }
22
-        url = '/api.php/records/posts?join=categories&join=tags&join=comments&filter=id,eq,1';
23 28
         req.open("GET", url, true);
24 29
         req.setRequestHeader('X-Authorization', 'Bearer '+accessToken);
25 30
         req.send();
26 31
     }
27 32
 };
33
+
34
+window.onload = function() {
35
+    requestAPI()
36
+    document.getElementById('request-btn').onclick = function(e) {
37
+        e.preventDefault()
38
+        requestAPI()
39
+    }
40
+}
28 41
 </script>
29 42
 </head>
30 43
 <body>
44
+<p>
45
+    <button type="button" id="request-btn">Request API</button>
46
+</p>
31 47
 <pre id="output"></pre>
32 48
 </body>
33 49
 </html>

Loading…
Cancel
Save