Skip to content

Commit bc1ff75

Browse files
Bind Session.authFetch so it survives being passed by reference (#11) (#12)
authFetch is the authenticated drop-in for fetch but was an unbound class method, so passing it by reference (rdflib Fetcher, solid-logic, rdf-dereference, or destructuring) lost the this binding and threw on this._isActive. Bind it in the constructor; add a test that destructures and calls it.
1 parent 9e9f240 commit bc1ff75

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

solid-oidc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,11 @@ export class Session extends EventTarget {
421421
this._idpDetails = null
422422
this._refreshPromise = null
423423

424+
// Bind so authFetch survives being passed by reference (drop-in fetch):
425+
// consumers hand it to rdflib / solid-logic / rdf-dereference as a bare
426+
// `fetch`, which would otherwise lose `this`.
427+
this.authFetch = this.authFetch.bind(this)
428+
424429
// Set up event listeners
425430
if (this.onStateChange) {
426431
this.addEventListener(SessionEvents.STATE_CHANGE, this.onStateChange)

test.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,23 @@ <h1>solid-oidc tests</h1>
256256
}
257257
})
258258

259+
asyncTest('Session: authFetch survives being passed by reference (bound)', async () => {
260+
const session = new Session()
261+
// Bare reference, the way rdflib / solid-logic / rdf-dereference take it.
262+
// Before binding this throws "Cannot read properties of undefined (reading '_isActive')".
263+
const { authFetch } = session
264+
const origFetch = globalThis.fetch
265+
globalThis.fetch = async () => new Response(null, { status: 200 })
266+
try {
267+
await authFetch('https://example.com', { method: 'HEAD' })
268+
assert(true, 'authFetch is callable when passed by reference')
269+
} catch (e) {
270+
assert(false, `authFetch lost its binding when passed by reference: ${e.message}`)
271+
} finally {
272+
globalThis.fetch = origFetch
273+
}
274+
})
275+
259276
// ==========================================================================
260277
// Summary
261278
// ==========================================================================

0 commit comments

Comments
 (0)