forked from auth0-samples/oidc-sso-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (66 loc) · 3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function displayStatus() {
var status;
const token = localStorage.getItem('accessToken');
const expirationDate = new Date(Number.parseInt(localStorage.getItem('expirationDate')));
const isExpired = expirationDate < new Date();
const profile = localStorage.getItem('profile');
if (!token) {
if (localStorage.getItem('sso')){
renew();
}
status = 'There is no access token present in local storage, meaning that you are not logged in. <a href="#" onclick="renew()">Click here to attempt an SSO login</a>';
} else if (isExpired) {
status = 'There is an expired access token in local storage. <a href="#" onclick="renew()">Click here to renew it</a>';
document.getElementById('logout').style.visibility = 'visible';
} else {
status = `There is an access token in local storage, and it expires on ${expirationDate}. <a href="#" onclick="renew()">Click here to renew it</a>`;
document.getElementById('logout').style.visibility = 'visible';
}
if (profile) {
const user = JSON.parse(profile);
status += `<p>Hello ${user.name}</p>
<image src="${user.picture}" alt="You don't have any picture"></image>
<p>Roles: ${user['https://epimodels.org/app_metadata'].roles}</p>
`;
}
document.getElementById('status').innerHTML = status;
}
function saveAuthResult (result) {
localStorage.setItem('accessToken', result.accessToken);
localStorage.setItem('expirationDate', Date.now() + Number.parseInt(result.expiresIn) * 1000);
auth0js.client.userInfo(result.accessToken, function(err, user) {
localStorage.setItem('profile', JSON.stringify(user));
displayStatus();
});
}
function renew () {
auth0js.renewAuth({
redirectUri: REDIRECT_URL,
usePostMessage: true
}, function (err, result) {
if (err || (result && result.error)) { // For auth0.js version 8.8, the error shows up at result.error
if (result)
err = {error: result.errorDescription}; // For auth0.js version 8.8, the error message shows up at result.errorDescription
alert(`Could not get a new token using silent authentication (${err.error}). Redirecting to login page...`);
// auth0js.authorize();
signOnViaMidasAccounts();
} else {
saveAuthResult(result);
}
});
}
displayStatus();
function signOnViaMidasAccounts() {
localStorage.setItem('sso', true);
window.location.href = toMidasAccountsUrl('/sso');
}
function signoff() {
localStorage.clear();
window.location.href = toMidasAccountsUrl('/signoff', 'You have been signed off. You may sign on again.');
}
function toMidasAccountsUrl(endpoint, message, title) {
title = title || "OIDC SSO Example";
message = message || "Please sign on to use the services";
return MIDAS_ACCOUNTS_URL + endpoint + '?returnToUrl='
+ encodeURIComponent(window.location) + '&title=' + title + '&message=' + message;
}