|
10 | 10 | <script src="https://cdn.developer.sas.com/packages/sas-auth-browser/latest/dist/index.js"></script>
|
11 | 11 | <script>
|
12 | 12 | (async () => {
|
13 |
| - const viyaUrl = "YOUR-VIYA-URL"; |
| 13 | + const content = document.getElementById('content'); |
| 14 | + const endpoint = 'YOUR-VIYA-URL'; |
| 15 | + |
| 16 | + function clearContent() { |
| 17 | + for (const child of [...content.childNodes]) { |
| 18 | + content.removeChild(child); |
| 19 | + } |
| 20 | + } |
| 21 | + function createLoginButton() { |
| 22 | + const btn = document.createElement('button'); |
| 23 | + btn.textContent = 'Logon...'; |
| 24 | + btn.onclick = async () => { |
| 25 | + await instance.loginPopup(); |
| 26 | + fetchFolders(); |
| 27 | + }; |
| 28 | + content.appendChild(btn); |
| 29 | + } |
14 | 30 | async function fetchFolders() {
|
15 |
| - const content = document.getElementById("content"); |
16 |
| - content.innerText = "Fetching folders."; |
| 31 | + content.innerText = 'Fetching folders.'; |
17 | 32 | let json;
|
18 | 33 | try {
|
19 | 34 | resp = await fetch(
|
20 |
| - `${viyaUrl}/folders/folders?limit=1000000&filter=and(isNull(parent),in(type,%27folder%27))`, |
| 35 | + `${endpoint}/folders/folders?limit=1000000&filter=and(isNull(parent),%20in(type,%27folder%27))`, |
21 | 36 | {
|
22 |
| - credentials: "include", |
| 37 | + credentials: 'include', |
23 | 38 | // REST requests from browsers should include this header.
|
24 |
| - "X-Requested-With": "XMLHttpRequest", |
| 39 | + 'X-Requested-With': 'XMLHttpRequest', |
25 | 40 | }
|
26 | 41 | );
|
27 | 42 | if (!resp.ok) {
|
28 |
| - throw new Error("Invalid response"); |
| 43 | + throw new Error('Invalid response'); |
29 | 44 | }
|
30 | 45 | json = await resp.json();
|
31 | 46 | } catch (e) {
|
32 |
| - content.innerText = "Unable to fetch folders."; |
| 47 | + content.innerText = 'Unable to fetch folders.'; |
33 | 48 | throw e;
|
34 | 49 | }
|
35 | 50 |
|
36 |
| - content.innerHTML = ""; |
37 |
| - const title = document.createElement("h1"); |
38 |
| - title.innerText = "Folders"; |
| 51 | + clearContent(); |
| 52 | + const title = document.createElement('h1'); |
| 53 | + title.innerText = 'Folders'; |
39 | 54 | content.appendChild(title);
|
40 |
| - const ul = document.createElement("ul"); |
| 55 | + const ul = document.createElement('ul'); |
41 | 56 | content.appendChild(ul);
|
42 | 57 | for (const folder of json?.items || []) {
|
43 |
| - const li = document.createElement("li"); |
| 58 | + const li = document.createElement('li'); |
44 | 59 | li.innerText = folder.name;
|
45 | 60 | ul.appendChild(li);
|
46 | 61 | }
|
| 62 | + const button = document.createElement('button'); |
| 63 | + button.textContent = 'Logout'; |
| 64 | + button.addEventListener('click', async () => { |
| 65 | + await instance.logout(); |
| 66 | + clearContent(); |
| 67 | + createLoginButton(); |
| 68 | + }); |
| 69 | + content.appendChild(button); |
47 | 70 | }
|
48 | 71 |
|
49 | 72 | const instance = sasAuthBrowser.createCookieAuthenticationCredential({
|
50 |
| - url: viyaUrl, |
| 73 | + url: endpoint, |
51 | 74 | });
|
52 | 75 | try {
|
53 |
| - // Before calling any SAS Viya API endpoints, first ensure that a user is authenticated. |
54 | 76 | await instance.checkAuthenticated();
|
55 |
| - // If a user is authenticated, it is safe to start making REST calls |
56 | 77 | fetchFolders();
|
57 | 78 | } catch {
|
58 |
| - // When no user is authenticated, create a button to prompt the user to sign in. |
59 |
| - // NOTE: A button is used so that the browser does not block the popup. |
60 |
| - const btn = document.createElement("button"); |
61 |
| - btn.textContent = "Logon..."; |
62 |
| - btn.onclick = async () => { |
63 |
| - // Open a popup and navigate to the logon endpoint. |
64 |
| - // Once the user has logged in, the popup will automatically close. |
65 |
| - await instance.loginPopup(); |
66 |
| - // After the user has successfully logged in and the popup has closed the promise |
67 |
| - // will resolve and REST calls can now be made to SAS Viya endpoints. |
68 |
| - fetchFolders(); |
69 |
| - }; |
70 |
| - const content = document.getElementById("content"); |
71 |
| - content.innerHTML = ""; |
72 |
| - content.appendChild(btn); |
| 79 | + clearContent(); |
| 80 | + createLoginButton(); |
73 | 81 | }
|
74 | 82 | })();
|
75 | 83 | </script>
|
|
0 commit comments