-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathauth-buttons.js
82 lines (75 loc) · 2.34 KB
/
auth-buttons.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
/* global location, alert, solid */
/* Provide functionality for authentication buttons */
((SessionManager) => {
// Wire up DOM elements
const [
loginButton,
logoutButton,
registerButton,
accountSettings,
loggedInContainer,
profileLink
] = [
'login',
'logout',
'register',
'account-settings',
'loggedIn',
'profileLink'
].map(id => document.getElementById(id) || document.createElement('a'))
loginButton.addEventListener('click', login)
logoutButton.addEventListener('click', logout)
registerButton.addEventListener('click', register)
function onSessionChange(sessionInfo) {
const loggedIn = sessionInfo.isLoggedIn
const isOwner = loggedIn && new URL(sessionInfo.webId).origin === location.origin
loginButton.classList.toggle('hidden', loggedIn)
logoutButton.classList.toggle('hidden', !loggedIn)
registerButton.classList.toggle('hidden', loggedIn)
accountSettings.classList.toggle('hidden', !isOwner)
loggedInContainer.classList.toggle('hidden', !loggedIn)
if (sessionInfo) {
profileLink.href = sessionInfo.webId
profileLink.innerText = sessionInfo.webId
}
}
const session = new SessionManager.Session(
{
clientAuthentication: solidClientAuthentication.getClientAuthenticationWithDependencies(
{}
),
},
"mySession"
);
const authCode = new URL(window.location.href).searchParams.get("code")
if (authCode) {
// Being redirected after requesting a token
session
.handleIncomingRedirect(new URL(window.location.href))
.then((sessionInfo) => {
onSessionChange(sessionInfo)
});
} else {
onSessionChange(session.info)
}
// Log the user in on the client and the server
async function login () {
// TODO: This should be made to look nicer.
const thisUrl = new URL(window.location.href).origin
const issuer = prompt("Enter an issuer", thisUrl)
session.login({
redirectUrl: new URL(window.location.href),
oidcIssuer: new URL(issuer),
});
}
// Log the user out from the client and the server
async function logout () {
await session.logout()
location.reload()
}
// Redirect to the registration page
function register () {
const registration = new URL('/register', location)
location.href = registration
}
})(solidClientAuthentication)