Skip to content
Merged
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
5 changes: 5 additions & 0 deletions solid-oidc.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,11 @@ export class Session extends EventTarget {
this._idpDetails = null
this._refreshPromise = null

// Bind so authFetch survives being passed by reference (drop-in fetch):
// consumers hand it to rdflib / solid-logic / rdf-dereference as a bare
// `fetch`, which would otherwise lose `this`.
this.authFetch = this.authFetch.bind(this)

// Set up event listeners
if (this.onStateChange) {
this.addEventListener(SessionEvents.STATE_CHANGE, this.onStateChange)
Expand Down
17 changes: 17 additions & 0 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,23 @@ <h1>solid-oidc tests</h1>
}
})

asyncTest('Session: authFetch survives being passed by reference (bound)', async () => {
const session = new Session()
// Bare reference, the way rdflib / solid-logic / rdf-dereference take it.
// Before binding this throws "Cannot read properties of undefined (reading '_isActive')".
const { authFetch } = session
const origFetch = globalThis.fetch
globalThis.fetch = async () => new Response(null, { status: 200 })
try {
await authFetch('https://example.com', { method: 'HEAD' })
assert(true, 'authFetch is callable when passed by reference')
} catch (e) {
assert(false, `authFetch lost its binding when passed by reference: ${e.message}`)
} finally {
globalThis.fetch = origFetch
}
})

// ==========================================================================
// Summary
// ==========================================================================
Expand Down