|
1 | 1 | /* global location, alert, solid */ |
2 | 2 | /* Provide functionality for authentication buttons */ |
3 | 3 |
|
4 | | -(({ auth }) => { |
| 4 | +((SessionManager) => { |
5 | 5 | // Wire up DOM elements |
6 | 6 | const [ |
7 | 7 | loginButton, |
|
22 | 22 | logoutButton.addEventListener('click', logout) |
23 | 23 | registerButton.addEventListener('click', register) |
24 | 24 |
|
25 | | - // Track authentication status and update UI |
26 | | - auth.trackSession(session => { |
27 | | - const loggedIn = !!session |
28 | | - const isOwner = loggedIn && new URL(session.webId).origin === location.origin |
| 25 | + function onSessionChange(sessionInfo) { |
| 26 | + const loggedIn = sessionInfo.isLoggedIn |
| 27 | + const isOwner = loggedIn && new URL(sessionInfo.webId).origin === location.origin |
29 | 28 | loginButton.classList.toggle('hidden', loggedIn) |
30 | 29 | logoutButton.classList.toggle('hidden', !loggedIn) |
31 | 30 | registerButton.classList.toggle('hidden', loggedIn) |
32 | 31 | accountSettings.classList.toggle('hidden', !isOwner) |
33 | 32 | loggedInContainer.classList.toggle('hidden', !loggedIn) |
34 | | - if (session) { |
35 | | - profileLink.href = session.webId |
36 | | - profileLink.innerText = session.webId |
| 33 | + if (sessionInfo) { |
| 34 | + profileLink.href = sessionInfo.webId |
| 35 | + profileLink.innerText = sessionInfo.webId |
37 | 36 | } |
38 | | - }) |
| 37 | + } |
| 38 | + |
| 39 | + const session = new SessionManager.Session( |
| 40 | + { |
| 41 | + clientAuthentication: solidClientAuthentication.getClientAuthenticationWithDependencies( |
| 42 | + {} |
| 43 | + ), |
| 44 | + }, |
| 45 | + "mySession" |
| 46 | + ); |
| 47 | + |
| 48 | + const authCode = new URL(window.location.href).searchParams.get("code") |
| 49 | + if (authCode) { |
| 50 | + // Being redirected after requesting a token |
| 51 | + session |
| 52 | + .handleIncomingRedirect(new URL(window.location.href)) |
| 53 | + .then((sessionInfo) => { |
| 54 | + onSessionChange(sessionInfo) |
| 55 | + }); |
| 56 | + } else { |
| 57 | + onSessionChange(session.info) |
| 58 | + } |
39 | 59 |
|
40 | 60 | // Log the user in on the client and the server |
41 | 61 | async function login () { |
42 | | - const session = await auth.popupLogin() |
43 | | - if (session) { |
44 | | - // Make authenticated request to the server to establish a session cookie |
45 | | - const { status } = await auth.fetch(location, { method: 'HEAD' }) |
46 | | - if (status === 401) { |
47 | | - alert(`Invalid login.\n\nDid you set ${session.idp} as your OIDC provider in your profile ${session.webId}?`) |
48 | | - await auth.logout() |
49 | | - } |
50 | | - location.reload() |
51 | | - } |
| 62 | + // TODO: This should be made to look nicer. |
| 63 | + const thisUrl = new URL(window.location.href).origin |
| 64 | + const issuer = prompt("Enter an issuer", thisUrl) |
| 65 | + session.login({ |
| 66 | + redirectUrl: new URL(window.location.href), |
| 67 | + oidcIssuer: new URL(issuer), |
| 68 | + }); |
52 | 69 | } |
53 | 70 |
|
54 | 71 | // Log the user out from the client and the server |
55 | 72 | async function logout () { |
56 | | - await auth.logout() |
| 73 | + await session.logout() |
57 | 74 | location.reload() |
58 | 75 | } |
59 | 76 |
|
|
62 | 79 | const registration = new URL('/register', location) |
63 | 80 | location.href = registration |
64 | 81 | } |
65 | | -})(solid) |
| 82 | +})(solidClientAuthentication) |
0 commit comments