Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
103 lines (90 loc) · 3.1 KB
/
Copy pathapp.js
File metadata and controls
103 lines (90 loc) · 3.1 KB
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
$('document').ready(function() {
var content = $('.content');
var loadingSpinner = $('#loading');
content.css('display', 'block');
loadingSpinner.css('display', 'none');;
var webAuth = new auth0.WebAuth({
domain: AUTH0_DOMAIN,
clientID: AUTH0_CLIENT_ID,
redirectUri: AUTH0_CALLBACK_URL,
audience: 'https://' + AUTH0_DOMAIN + '/userinfo',
responseType: 'token id_token',
scope: 'openid',
leeway: 60
});
var loginStatus = $('.container h4');
var loginView = $('#login-view');
var homeView = $('#home-view');
// buttons and event listeners
var homeViewBtn = $('#btn-home-view');
var loginBtn = $('#btn-login');
var killSessionBtn = $('#btn-kill-session');
var logoutBtn = $('#btn-logout');
homeViewBtn.click(function() {
homeView.css('display', 'inline-block');
loginView.css('display', 'none');
});
loginBtn.click(function(e) {
e.preventDefault();
webAuth.authorize();
});
killSessionBtn.click(killSession);
logoutBtn.click(logout);
function setSession(authResult) {
// Set the time that the access token will expire at
var expiresAt = JSON.stringify(
authResult.expiresIn * 1000 + new Date().getTime()
);
localStorage.setItem('access_token', authResult.accessToken);
localStorage.setItem('id_token', authResult.idToken);
localStorage.setItem('expires_at', expiresAt);
}
function killSession() {
// Remove tokens and expiry time from localStorage
localStorage.removeItem('access_token');
localStorage.removeItem('id_token');
localStorage.removeItem('expires_at');
displayButtons();
}
function logout() {
// Remove tokens and expiry time from localStorage
killSession();
var origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
window.location.href = 'https://'+AUTH0_DOMAIN+'/v2/logout?returnTo='+encodeURIComponent(origin);
}
function isAuthenticated() {
// Check whether the current time is past the
// access token's expiry time
var expiresAt = JSON.parse(localStorage.getItem('expires_at'));
return new Date().getTime() < expiresAt;
}
function handleAuthentication() {
webAuth.parseHash(function(err, authResult) {
if (authResult && authResult.accessToken && authResult.idToken) {
window.location.hash = '';
setSession(authResult);
loginBtn.css('display', 'none');
homeView.css('display', 'inline-block');
} else if (err) {
homeView.css('display', 'inline-block');
console.log(err);
alert(
'Error: ' + err.error + '. Check the console for further details.'
);
}
displayButtons();
});
}
function displayButtons() {
if (isAuthenticated()) {
loginBtn.css('display', 'none');
logoutBtn.css('display', 'inline-block');
loginStatus.text('You are logged in!');
} else {
loginBtn.css('display', 'inline-block');
logoutBtn.css('display', 'none');
loginStatus.text('You are not logged in! Please log in to continue.');
}
}
handleAuthentication();
});