You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While this issue is specific to using a custom theme (which you may consider unsupported), the same problem would occur if keycloak-js was used in another app.
Version
26.4.2
Expected behavior
Updating a patch release should not break downstream code
Actual behavior
The change to login means that code like this
const{ login }=context.keycloak;// keycloak is an instance of keycloak-js// somewhere down the linelogin();
now fails with an error like
Uncaught TypeError: Cannot read properties of undefined (reading '#t')
at login (KcAccountUi-BGDGSLdZ.js:17:5881)
at onClick (SigningIn-DLddgWMK.js:1:3154)
at Object.Fa (index-DwT4Nu4t.js:38:9830)
at Aa (index-DwT4Nu4t.js:38:9984)
at Va (index-DwT4Nu4t.js:38:10041)
at Ai (index-DwT4Nu4t.js:38:31401)
at Vi (index-DwT4Nu4t.js:38:31818)
at index-DwT4Nu4t.js:38:36749
at mo (index-DwT4Nu4t.js:41:37065)
at Ko (index-DwT4Nu4t.js:38:8967)
where #t is the mangled name of #adapter meaning this is undefined inside login.
While this issue is specific to custom themes (which you might consider unsupported), the same problem would occur if keycloak-js were used in any other app.
Let me clarify what’s happening here.
The bug will surface in the Keycloak Account Console as soon as version 26.2.1 of keycloak-js is bundled with Keycloak.
Keycloakify users are simply encountering it early.
This is, fully expected to fail. It's not a bug in Keycloak-js. When dealing with class instance methods, it’s important to call them with the correct context. Either with keycloak.login() or with login.call(keycloak).
If keycloak-js had always behaved this way, that would be fine, it’s standard JavaScript OOP. However, since keycloak-js is an infrastructure-level library and this incorrect usage even appears in the Keycloak core, it might be worth “fixing” it here to minimize disruption.
@jonkoops, this currently breaks all account themes started today and any that update keycloak-js to the latest patch.
Should we fix this downstream or here?
Please let me know so I can communicate accordingly and if this won't be fixed here I need to apply a patch to keycloak's code while repackaging ASAP.
Hi all, yes, destructuring is not supported here, as it changes the this value when calling methods. Previously, this would not be an issue, as we were scoping private variables in a function, and not using true classes.
This demonstrates the issue we're talking about:
classPerson{constructor(name){this.name=name;}sayName(){console.log(this.name);}}// Works, the 'name' property can be found on 'this'.constperson=newPerson("John Doe");person.sayName();// Breaks, 'this' is now orphaned, and therefore 'undefined'.const{ sayName }=person;sayName();
this currently breaks all account themes started today and any that update keycloak-js to the latest patch.
Should we fix this downstream or here?
@garronej I think we should be fixing this in both, I intended this refactor to be a patch that has basically no breaking changes (as much as that is possible), but failed to take this into account. I think I'll prepare a patch that makes all the methods' this value expicitly bound by using arrow functions (really wish this was the default behaviour for class methods):
classPerson{constructor(name){this.name=name;}sayName=()=>{console.log(this.name);}}// Works, the 'name' property can be found on 'this'.constperson=newPerson("John Doe");person.sayName();// Also, works, as arrow functions are always bound to the 'this' on decleration.const{ sayName }=person;sayName();// Re-binding 'this' doesn't do anything either, this will still log "John Doe".person.sayName.call({name: "Jane Doe"});
The only breaking change I can see here is if users are re-binding the method using .call(), .apply() or .bind(). The only reason I can imagine for doing so would be to call methods on some internal state one would not want to mutate. However, that is a minor, and frankly strange use-case, I am willing to compromise on to get destructuring to work properly.
The only breaking change I can see here is if users are re-binding the method using .call(), .apply(), or .bind(). The only reason I can imagine for doing so would be to call methods on some internal state one would not want to mutate. However, that is a minor, and frankly strange, use case I’m willing to compromise on to get destructuring to work properly.
Yeah, I had the same thought. I really doubt anyone is actually swapping the context with .bind() or .call() in the wild, keycloak-js is mostly used as a singleton anyway. But many people do destructure the methods, so I think the trade-off on theorical corectness is worth it.
And yeah your fix was definately the correct one, my suggested approach involving a getters would have introduced a needless level of indirection, just turning methods into arrow functions properties was the obvious good way to go about it. 👍🏻
Activity
garronej commentedon Oct 30, 2025
Thank you @kodebach for investigating this!
Let me clarify what’s happening here.
The bug will surface in the Keycloak Account Console as soon as version 26.2.1 of
keycloak-jsis bundled with Keycloak.Keycloakify users are simply encountering it early.
What’s happening
Consider the following code:
This is, fully expected to fail. It's not a bug in Keycloak-js. When dealing with class instance methods, it’s important to call them with the correct context. Either with
keycloak.login()or withlogin.call(keycloak).If
keycloak-jshad always behaved this way, that would be fine, it’s standard JavaScript OOP. However, sincekeycloak-jsis an infrastructure-level library and this incorrect usage even appears in the Keycloak core, it might be worth “fixing” it here to minimize disruption.Possible fix
Replace:
With:
@jonkoops, this currently breaks all account themes started today and any that update
keycloak-jsto the latest patch.Should we fix this downstream or here?
Please let me know so I can communicate accordingly and if this won't be fixed here I need to apply a patch to keycloak's code while repackaging ASAP.
Thank you for your great work.
https://github.com/keycloak/keycloak-js/issues/202
jonkoops commentedon Nov 4, 2025
Hi all, yes, destructuring is not supported here, as it changes the
thisvalue when calling methods. Previously, this would not be an issue, as we were scoping private variables in a function, and not using true classes.This demonstrates the issue we're talking about:
@garronej I think we should be fixing this in both, I intended this refactor to be a patch that has basically no breaking changes (as much as that is possible), but failed to take this into account. I think I'll prepare a patch that makes all the methods'
thisvalue expicitly bound by using arrow functions (really wish this was the default behaviour for class methods):The only breaking change I can see here is if users are re-binding the method using
.call(),.apply()or.bind(). The only reason I can imagine for doing so would be to call methods on some internal state one would not want to mutate. However, that is a minor, and frankly strange use-case, I am willing to compromise on to get destructuring to work properly.WDYT?
Bind public methods to class instance
jonkoops commentedon Nov 4, 2025
PR up under #204
garronej commentedon Nov 4, 2025
Hey @jonkoops,
I agree with the conclusion.
Yeah, I had the same thought. I really doubt anyone is actually swapping the context with
.bind()or.call()in the wild,keycloak-jsis mostly used as a singleton anyway. But many people do destructure the methods, so I think the trade-off on theorical corectness is worth it.jonkoops commentedon Nov 4, 2025
Nice, I merged the PR, there will likely be a couple more small fixes, after which we'll do a 26.2.2 release.
Bind public methods to class instance (keycloak#204)
garronej commentedon Nov 4, 2025
Thank you very much @jonkoops for the quick fix!
And yeah your fix was definately the correct one, my suggested approach involving a getters would have introduced a needless level of indirection, just turning methods into arrow functions properties was the obvious good way to go about it. 👍🏻
Best,
mm-ruhri commentedon Nov 24, 2025
Thanks for fixing!
Is there a chance, that the fix is released in near future?
jonkoops commentedon Nov 25, 2025
Yes, I will hopefully get a release out the door this week, we're working on a couple more bugfixes before doing a release.