diff --git a/solid-oidc.js b/solid-oidc.js index a2938be..be46edb 100644 --- a/solid-oidc.js +++ b/solid-oidc.js @@ -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) diff --git a/test.html b/test.html index 023242d..3e8bbb4 100644 --- a/test.html +++ b/test.html @@ -256,6 +256,23 @@

solid-oidc tests

} }) + 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 // ==========================================================================