Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 38 additions & 21 deletions common/js/auth-buttons.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global location, alert, solid */
/* Provide functionality for authentication buttons */

(({ auth }) => {
((SessionManager) => {
// Wire up DOM elements
const [
loginButton,
Expand All @@ -22,38 +22,55 @@
logoutButton.addEventListener('click', logout)
registerButton.addEventListener('click', register)

// Track authentication status and update UI
auth.trackSession(session => {
const loggedIn = !!session
const isOwner = loggedIn && new URL(session.webId).origin === location.origin
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 (session) {
profileLink.href = session.webId
profileLink.innerText = session.webId
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 () {
const session = await auth.popupLogin()
if (session) {
// Make authenticated request to the server to establish a session cookie
const { status } = await auth.fetch(location, { method: 'HEAD' })
if (status === 401) {
alert(`Invalid login.\n\nDid you set ${session.idp} as your OIDC provider in your profile ${session.webId}?`)
await auth.logout()
}
location.reload()
}
// 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 auth.logout()
await session.logout()
location.reload()
}

Expand All @@ -62,4 +79,4 @@
const registration = new URL('/register', location)
location.href = registration
}
})(solid)
})(solidClientAuthentication)
2 changes: 1 addition & 1 deletion default-templates/server/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ <h2>Server info</h2>
</dl>
</section>
</div>
<script src="/common/js/solid-auth-client.bundle.js"></script>
<script src="/common/js/solid-client-authn.bundle.js"></script>
<script src="/common/js/auth-buttons.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion default-views/auth/login-required.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

</div>
</div>
<script src="/common/js/solid-auth-client.bundle.js"></script>
<script src="/common/js/solid-client-authn.bundle.js"></script>
<script src="/common/js/auth-buttons.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion default-views/auth/no-permission.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</div>
</div>
</div>
<script src="/common/js/solid-auth-client.bundle.js"></script>
<script src="/common/js/solid-client-authn.bundle.js"></script>
<script src="/common/js/auth-buttons.js"></script>
</body>
</html>
3 changes: 1 addition & 2 deletions lib/create-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ function createApp (argv = {}) {
// Serve the public 'common' directory (for shared CSS files, etc)
app.use('/common', express.static(path.join(__dirname, '../common')))
app.use('/', express.static(path.dirname(require.resolve('mashlib/dist/databrowser.html')), { index: false }))
routeResolvedFile(app, '/common/js/', 'solid-auth-client/dist-lib/solid-auth-client.bundle.js')
routeResolvedFile(app, '/common/js/', 'solid-auth-client/dist-lib/solid-auth-client.bundle.js.map')
routeResolvedFile(app, '/common/js/', '@inrupt/solid-client-authn-browser/browserDist/solid-client-authn.bundle.js')
app.use('/.well-known', express.static(path.join(__dirname, '../common/well-known')))

// Serve bootstrap from it's node_module directory
Expand Down
Loading