Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 14 additions & 1 deletion jest.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import { existsSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const localSolidLogicSrc = path.resolve(__dirname, '../solid-logic/src')
const solidLogicMapper = existsSync(localSolidLogicSrc)
? localSolidLogicSrc
: '<rootDir>/node_modules/solid-logic/src'

export default {
// verbose: true, // Uncomment for detailed test output
collectCoverage: true,
Expand All @@ -16,7 +27,9 @@ export default {
setupFilesAfterEnv: ['./test/helpers/setup.ts'],
moduleNameMapper: {
'^~icons/(.*)$': '<rootDir>/__mocks__/iconsMock.js',
'^.+\\.css$': '<rootDir>/__mocks__/styleMock.js'
'^.+\\.css$': '<rootDir>/__mocks__/styleMock.js',
'^solid-logic$': solidLogicMapper,
'^@uvdsl/solid-oidc-client-browser$': '<rootDir>/test/mocks/solid-oidc-client-browser.ts'
},
Comment on lines 28 to 33
testMatch: ['**/?(*.)+(spec|test).[tj]s?(x)'],
roots: ['<rootDir>/src', '<rootDir>/test', '<rootDir>/__mocks__'],
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "solid-ui",
"version": "3.1.1",
"version": "3.1.2",
"description": "UI library for Solid applications",
"main": "dist/solid-ui.js",
"types": "dist/index.d.ts",
Expand Down
55 changes: 40 additions & 15 deletions src/design-system/lib/auth/SolidAuth.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
import { authSession, solidLogicSingleton } from 'solid-logic'
import Account from '../../../primitives/lib/auth/Account'
import { findImage } from '../../../widgets/buttons'
import { AuthContext } from '../../../primitives/lib/auth/context'
import { showDialog } from '../dialogs'
import { html } from 'lit'
import ns from '../../../ns'

import '../../components/login-modal'

export const DEFAULT_SIGNUP_URL = 'https://solidproject.org/get_a_pod'

function findAccountImage (webId: string): string | undefined {
const store = solidLogicSingleton.store
const me = store.sym(webId)
const image =
store.any(me, ns.sioc('avatar')) ||
store.any(me, ns.foaf('img')) ||
store.any(me, ns.vcard('logo')) ||
store.any(me, ns.vcard('hasPhoto')) ||
store.any(me, ns.vcard('photo')) ||
store.any(me, ns.foaf('depiction'))

return image ? (image as any).value : undefined
}

export default class SolidAuth implements AuthContext {
constructor (public signupUrl: string = DEFAULT_SIGNUP_URL) {}

get account (): Account | null {
if (!authSession.info?.isLoggedIn || !authSession.info?.webId) {
const sessionAny = authSession as any
const webId: string | undefined = sessionAny.webId ?? sessionAny.info?.webId
const isActive: boolean = sessionAny.isActive ?? sessionAny.info?.isLoggedIn ?? Boolean(webId)
if (!isActive || !webId) {
return null
}

const webId = authSession.info.webId
const me = solidLogicSingleton.store.sym(webId)
const avatarUrl = findImage(me) ?? undefined
const avatarUrl = findAccountImage(webId)

return new Account(webId, avatarUrl)
}
Expand All @@ -44,10 +59,7 @@ export default class SolidAuth implements AuthContext {

locationUrl.hash = ''

await authSession.login({
redirectUrl: locationUrl.href,
oidcIssuer: loginUrl
})
await (authSession as any).login(loginUrl, locationUrl.href)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should stop using any unless there is a very good reason for it (such as using a 3rd party library that is poorly typed, or some encapsulated type check that is not worth typing). In this case, since this comes from solid-logic which is a library we control, it should be possible to type correctly, right? It's also something quite important, so I would avoid any to reduce the potential for bugs as much as possible.

}

async signup () {
Expand All @@ -59,14 +71,27 @@ export default class SolidAuth implements AuthContext {
}

onSessionUpdated (callback: () => unknown) {
authSession.events.on('login', callback)
authSession.events.on('logout', callback)
authSession.events.on('sessionRestore', callback)
const sessionEventTarget = authSession as unknown as EventTarget
const sessionAny = authSession as any
const listener = () => {
callback()
}
if (typeof sessionEventTarget.addEventListener === 'function') {
sessionEventTarget.addEventListener('sessionStateChange', listener)
} else {
sessionAny.events.on('login', callback)
sessionAny.events.on('logout', callback)
sessionAny.events.on('sessionRestore', callback)
}

return () => {
authSession.events.off('login', callback)
authSession.events.off('logout', callback)
authSession.events.off('sessionRestore', callback)
if (typeof sessionEventTarget.removeEventListener === 'function') {
sessionEventTarget.removeEventListener('sessionStateChange', listener)
} else {
sessionAny.events.off('login', callback)
sessionAny.events.off('logout', callback)
sessionAny.events.off('sessionRestore', callback)
}
}
}
}
35 changes: 14 additions & 21 deletions src/login/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ export async function ensureLoadedPreferences (
} else {
throw new Error(`(via loadPrefs) ${err}`)
}

context.preferencesFileError = m2
}
return context
}
Expand Down Expand Up @@ -513,10 +515,7 @@ export function renderSignInPopup (dom: HTMLDocument) {
// Login
const locationUrl = new URL(window.location.href)
locationUrl.hash = '' // remove hash part
await authSession.login({
redirectUrl: locationUrl.href,
oidcIssuer: issuerUri
})
await authSession.login(issuerUri, locationUrl.href)
} catch (err) {
alert(err.message)
}
Expand Down Expand Up @@ -669,9 +668,9 @@ export function loginStatusBox (
}

box.refresh = function () {
const sessionInfo = authSession.info
if (sessionInfo && sessionInfo.webId && sessionInfo.isLoggedIn) {
me = solidLogicSingleton.store.sym(sessionInfo.webId)
const webId = authSession.webId
if (webId) {
me = solidLogicSingleton.store.sym(webId)
} else {
me = null
}
Expand Down Expand Up @@ -716,6 +715,12 @@ authSession.events.on('logout', async () => {
await fetch(openidConfiguration.end_session_endpoint, { credentials: 'include' })
}
}

try {
await fetch('/.well-known/solid/logout', { credentials: 'include' })
} catch (_err) {
// Not all deployments expose NSS-compatible well-known logout endpoint.
}
} catch (_err) {
// Do nothing
}
Expand Down Expand Up @@ -1047,22 +1052,10 @@ export function newAppInstance (
* and/or a developer
*/
export async function getUserRoles (): Promise<Array<NamedNode>> {
const sessionInfo = authSession.info
if (!sessionInfo?.isLoggedIn || !sessionInfo?.webId) {
return []
}

const currentUser = authn.currentUser()
if (!currentUser) {
return []
}

try {
const { me, preferencesFile, preferencesFileError } = await ensureLoadedPreferences({
me: currentUser
})
const { me, preferencesFile, preferencesFileError } = await ensureLoadedPreferences({})
if (!preferencesFile || preferencesFileError) {
throw new Error(preferencesFileError || 'Unable to load user preferences file.')
throw new Error(preferencesFileError)
}
return solidLogicSingleton.store.each(
me,
Expand Down
5 changes: 1 addition & 4 deletions src/v2/components/auth/loginButton/LoginButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,7 @@ export class LoginButton extends LitElement {

const locationUrl = new URL(window.location.href)
locationUrl.hash = ''
await authSession.login({
redirectUrl: locationUrl.href,
oidcIssuer: issuerUri
})
await authSession.login(issuerUri, locationUrl.href)
Comment thread
bourgeoa marked this conversation as resolved.
} catch (err: any) {
this._errorMsg = err.message || String(err)
this.requestUpdate()
Expand Down
3 changes: 0 additions & 3 deletions src/v2/components/layout/footer/Footer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ export class Footer extends LitElement {
if (typeof authSession.events.off === 'function') {
authSession.events.off('login', this._updateFooter)
authSession.events.off('logout', this._updateFooter)
} else if (typeof authSession.events.removeListener === 'function') {
authSession.events.removeListener('login', this._updateFooter)
authSession.events.removeListener('logout', this._updateFooter)
}
super.disconnectedCallback()
}
Expand Down
Loading