-
Notifications
You must be signed in to change notification settings - Fork 347
/
app.js
124 lines (108 loc) · 3.49 KB
/
app.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
window.addEventListener('load', function() {
var idToken;
var accessToken;
var expiresAt;
var content = document.querySelector('.content');
var loadingSpinner = document.getElementById('loading');
content.style.display = 'block';
loadingSpinner.style.display = 'none';
var webAuth = new auth0.WebAuth({
domain: AUTH0_DOMAIN,
clientID: AUTH0_CLIENT_ID,
redirectUri: AUTH0_CALLBACK_URL,
responseType: 'token id_token',
scope: 'openid',
leeway: 60
});
var loginStatus = document.querySelector('.container h4');
var loginView = document.getElementById('login-view');
var homeView = document.getElementById('home-view');
// buttons and event listeners
var homeViewBtn = document.getElementById('btn-home-view');
var loginBtn = document.getElementById('qsLoginBtn');
var logoutBtn = document.getElementById('qsLogoutBtn');
homeViewBtn.addEventListener('click', function() {
homeView.style.display = 'inline-block';
loginView.style.display = 'none';
});
loginBtn.addEventListener('click', function(e) {
e.preventDefault();
webAuth.authorize();
});
logoutBtn.addEventListener('click', logout);
function localLogin(authResult) {
// Set isLoggedIn flag in localStorage
localStorage.setItem('isLoggedIn', 'true');
// Set the time that the access token will expire at
expiresAt = JSON.stringify(
authResult.expiresIn * 1000 + new Date().getTime()
);
accessToken = authResult.accessToken;
idToken = authResult.idToken;
}
function renewTokens() {
webAuth.checkSession({}, (err, authResult) => {
if (authResult && authResult.accessToken && authResult.idToken) {
localLogin(authResult);
} else if (err) {
alert(
'Could not get a new token ' + err.error + ':' + err.error_description + '.'
);
logout();
}
displayButtons();
});
}
function logout() {
// Remove isLoggedIn flag from localStorage
localStorage.removeItem('isLoggedIn');
// Remove tokens and expiry time
accessToken = '';
idToken = '';
expiresAt = 0;
webAuth.logout({
return_to: window.location.origin
});
displayButtons();
}
function isAuthenticated() {
// Check whether the current time is past the
// access token's expiry time
var expiration = parseInt(expiresAt) || 0;
return localStorage.getItem('isLoggedIn') === 'true' && new Date().getTime() < expiration;
}
function handleAuthentication() {
webAuth.parseHash(function(err, authResult) {
if (authResult && authResult.accessToken && authResult.idToken) {
window.location.hash = '';
localLogin(authResult);
loginBtn.style.display = 'none';
homeView.style.display = 'inline-block';
} else if (err) {
homeView.style.display = 'inline-block';
console.log(err);
alert(
'Error: ' + err.error + '. Check the console for further details.'
);
}
displayButtons();
});
}
function displayButtons() {
if (isAuthenticated()) {
loginBtn.style.display = 'none';
logoutBtn.style.display = 'inline-block';
loginStatus.innerHTML = 'You are logged in!';
} else {
loginBtn.style.display = 'inline-block';
logoutBtn.style.display = 'none';
loginStatus.innerHTML =
'You are not logged in! Please log in to continue.';
}
}
if (localStorage.getItem('isLoggedIn') === 'true') {
renewTokens();
} else {
handleAuthentication();
}
});