Skip to content

Instantly share code, notes, and snippets.

@stevehobbsdev
Created April 27, 2021 09:37
Show Gist options
  • Save stevehobbsdev/5d883075c4862ec07df714189c35565a to your computer and use it in GitHub Desktop.
Save stevehobbsdev/5d883075c4862ec07df714189c35565a to your computer and use it in GitHub Desktop.
A login/logout example using SPA SDK from the CDN. Put this content into an HTML file and serve on localhost:3000.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Test</title>
</head>
<body>
<button id="login">Log in</button>
<button id="logout">Log out</button>
<script src="https://cdn.auth0.com/js/auth0-spa-js/1.13/auth0-spa-js.production.js"></script>
<script>
const auth0 = new Auth0Client({
domain: 'elkdanger.eu.auth0.com',
client_id: 'TgqxTDJFG2G3H5TjqvIEEVL5i6IcZGye',
redirect_uri: window.location.origin
});
const logoutButton = document.getElementById('logout');
const loginButton = document.getElementById('login');
logoutButton.addEventListener('click', () => {
auth0.logout({ returnTo: window.location.origin });
});
loginButton.addEventListener('click', () => {
auth0.loginWithRedirect();
});
auth0
.getTokenSilently({})
.then(function (token) {
console.log(token);
})
.catch(function (error) {
if (error.error !== 'login_required') {
console.error(error);
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment