Created
December 13, 2017 02:36
-
-
Save aaguiarz/36c94de8c4e9637517affc3972676738 to your computer and use it in GitHub Desktop.
Lock v11 Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Lock 11 Example with Redirect</title> | |
</head> | |
<body> | |
<script src="https://cdn.auth0.com/js/lock/11.0.0-beta.8/lock.min.js"></script> | |
<script> | |
var tokenRenewalTimeout; | |
var lock = new Auth0Lock('WFyt09qw3MYQYVfhZ0Diz4c2jkVzozUs', 'aaguiar.auth0.com', | |
{ | |
autoclose: true, | |
auth: { | |
redirectUrl: 'http://localhost:8080/lock_v11.html', | |
responseType: 'token id_token', | |
audience: 'https://aaguiar.auth0.com/userinfo', | |
params: { | |
scope: 'openid email profile' | |
} | |
} | |
} | |
); | |
lock.on('authenticated', function(authResult) { | |
if (authResult && authResult.accessToken && authResult.idToken) { | |
setSession(authResult); | |
// When calling any API use access token | |
lock.getUserInfo(authResult.accessToken, function(err, userInfo) { | |
console.log("Hi", userInfo); | |
}); | |
// our just use authResult.idTokenPayload | |
} | |
}); | |
function login() | |
{ | |
lock.show(); | |
} | |
function renewToken() { | |
lock.checkSession({}, function(err, result) { | |
if (!err) { | |
setSession(result); | |
} | |
}); | |
} | |
function logout() { | |
// Remove tokens and expiry time from localStorage | |
localStorage.removeItem('access_token'); | |
localStorage.removeItem('id_token'); | |
localStorage.removeItem('expires_at'); | |
clearTimeout(tokenRenewalTimeout); | |
} | |
function setSession(authResult) | |
{ | |
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); | |
scheduleRenewal(); | |
} | |
function scheduleRenewal() { | |
var expiresAt = JSON.parse(localStorage.getItem('expires_at')); | |
var delay = expiresAt - Date.now(); | |
if (delay > 0) { | |
tokenRenewalTimeout = setTimeout(function() { | |
renewToken(); | |
}, delay); | |
} | |
} | |
</script> | |
<button onclick="login()">Login</button> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment