Skip to content

Commit 730eefb

Browse files
authored
feat(sas-browser-auth): Add logout endpoint call for cookie based authentication (#53)
1 parent d1e3715 commit 730eefb

File tree

3 files changed

+49
-31
lines changed

3 files changed

+49
-31
lines changed

sdk/sas-auth-browser/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.2.0 (Sep 26, 2023)
2+
3+
- Add logout function
4+
15
## 0.1.2 (May 16, 2023)
26

37
- fix guest authentication

sdk/sas-auth-browser/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ Launches a popup window that navigates to the authentication endpoint and allows
8080

8181
rejects: When login fails (for example: user closes the popup).
8282

83+
##### `logout(): Promise<void>`
84+
85+
Makes an endpoint call to the SAS Viya server that ends the cookie based browser session.
86+
87+
rejects: When sign out fails.
88+
8389
##### `invalidateCache()`
8490

8591
If `checkAuthenticated` has already been called, a cached value may be returned the next time. This is done to reduce the number of potential service calls needed when making multiple API calls. Calling `invalidateCache` will force `checkAuthenticated` to re-validate the next time it is called. **This is not needed for most use cases.**

sdk/sas-auth-browser/examples/basic-example.html

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,66 +10,74 @@
1010
<script src="https://cdn.developer.sas.com/packages/sas-auth-browser/latest/dist/index.js"></script>
1111
<script>
1212
(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+
}
1430
async function fetchFolders() {
15-
const content = document.getElementById("content");
16-
content.innerText = "Fetching folders.";
31+
content.innerText = 'Fetching folders.';
1732
let json;
1833
try {
1934
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))`,
2136
{
22-
credentials: "include",
37+
credentials: 'include',
2338
// REST requests from browsers should include this header.
24-
"X-Requested-With": "XMLHttpRequest",
39+
'X-Requested-With': 'XMLHttpRequest',
2540
}
2641
);
2742
if (!resp.ok) {
28-
throw new Error("Invalid response");
43+
throw new Error('Invalid response');
2944
}
3045
json = await resp.json();
3146
} catch (e) {
32-
content.innerText = "Unable to fetch folders.";
47+
content.innerText = 'Unable to fetch folders.';
3348
throw e;
3449
}
3550

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';
3954
content.appendChild(title);
40-
const ul = document.createElement("ul");
55+
const ul = document.createElement('ul');
4156
content.appendChild(ul);
4257
for (const folder of json?.items || []) {
43-
const li = document.createElement("li");
58+
const li = document.createElement('li');
4459
li.innerText = folder.name;
4560
ul.appendChild(li);
4661
}
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);
4770
}
4871

4972
const instance = sasAuthBrowser.createCookieAuthenticationCredential({
50-
url: viyaUrl,
73+
url: endpoint,
5174
});
5275
try {
53-
// Before calling any SAS Viya API endpoints, first ensure that a user is authenticated.
5476
await instance.checkAuthenticated();
55-
// If a user is authenticated, it is safe to start making REST calls
5677
fetchFolders();
5778
} 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();
7381
}
7482
})();
7583
</script>

0 commit comments

Comments
 (0)