-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth0_login.js
49 lines (45 loc) · 1.65 KB
/
auth0_login.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
auth0.createAuth0Client({
domain: "dev-rahbzx4kk45i122t.us.auth0.com",
clientId: "WF1yQFvwFjuWlGZtyG7E4NXD1qPgGeYA",
authorizationParams: {
redirect_uri: window.location.origin
}
}).then(async (auth0Client) => {
// Assumes a button with id "login" in the DOM
const loginButton = document.getElementById("login");
console.log('got the login button')
loginButton.addEventListener("click", (e) => {
e.preventDefault();
auth0Client.loginWithRedirect();
});
if (location.search.includes("state=") &&
(location.search.includes("code=") ||
location.search.includes("error="))) {
await auth0Client.handleRedirectCallback();
window.history.replaceState({}, document.title, "/");
}
// Assumes a button with id "logout" in the DOM
const logoutButton = document.getElementById("logout");
console.log('got the logout button')
logoutButton.addEventListener("click", (e) => {
e.preventDefault();
auth0Client.logout();
});
const isAuthenticated = await auth0Client.isAuthenticated();
console.log('isAuthenticated')
console.log(isAuthenticated)
const userProfile = await auth0Client.getUser();
console.log('userProfile')
console.log(userProfile)
// Assumes an element with id "profile" in the DOM
const profileElement = document.getElementById("profile");
if (isAuthenticated) {
profileElement.style.display = "block";
profileElement.innerHTML = `
<p>${userProfile.name}</p>
<img src="${userProfile.picture}" />
`;
} else {
profileElement.style.display = "none";
}
});