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
 <html>
1
 <html>
2
 <head>
2
 <head>
3
-<meta charset="utf-8" /> 
3
+<meta charset="utf-8" />
4
 <script>
4
 <script>
5
 var authUrl = 'https://php-crud-api.auth0.com/authorize'; // url of auth0 '/authorize' end-point
5
 var authUrl = 'https://php-crud-api.auth0.com/authorize'; // url of auth0 '/authorize' end-point
6
 var clientId = ''; // client id as defined in auth0
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
     var match = RegExp('[#&]access_token=([^&]*)').exec(window.location.hash);
11
     var match = RegExp('[#&]access_token=([^&]*)').exec(window.location.hash);
10
     var accessToken = match && decodeURIComponent(match[1].replace(/\+/g, ' '));
12
     var accessToken = match && decodeURIComponent(match[1].replace(/\+/g, ' '));
11
     if (!accessToken) {
13
     if (!accessToken) {
16
         req.onreadystatechange = function () {
18
         req.onreadystatechange = function () {
17
             if (req.readyState==4) {
19
             if (req.readyState==4) {
18
                 console.log(req.responseText);
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
         req.open("GET", url, true);
28
         req.open("GET", url, true);
24
         req.setRequestHeader('X-Authorization', 'Bearer '+accessToken);
29
         req.setRequestHeader('X-Authorization', 'Bearer '+accessToken);
25
         req.send();
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
 </script>
41
 </script>
29
 </head>
42
 </head>
30
 <body>
43
 <body>
44
+<p>
45
+    <button type="button" id="request-btn">Request API</button>
46
+</p>
31
 <pre id="output"></pre>
47
 <pre id="output"></pre>
32
 </body>
48
 </body>
33
 </html>
49
 </html>

Loading…
Cancel
Save