Skip to content

Release 26.2.1 contains breaking changes #202

Closed

Description

@kodebach

Describe the bug

Specifically this commit 1e9576a caused keycloakify/keycloakify#946

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 line
login();

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.

How to Reproduce?

No response

Anything else?

No response

Activity

  1. garronej commented on Oct 30, 2025

    @garronej

    Thank you @kodebach for investigating this!

    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.


    What’s happening

    Consider the following code:

    const keycloak = new Keycloak();
    const { login } = keycloak;
    
    login();

    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.


    Possible fix

    Replace:

    /**
     * @param {KeycloakLoginOptions} [options]
     * @returns {Promise<void>}
     */
    login(options) {
      return this.#adapter.login(options);
    }

    With:

    get login() {
      return options => this.#adapter.login(options);
    }

    @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.

    Thank you for your great work.

  2. jonkoops commented on Nov 4, 2025

    @jonkoops
    Contributor

    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:

    class Person {
      constructor(name) {
        this.name = name;
      }
    
      sayName() {
        console.log(this.name);
      }
    }
    
    // Works, the 'name' property can be found on 'this'.
    const person = new Person("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):

    class Person {
      constructor(name) {
        this.name = name;
      }
    
      sayName = () => {
        console.log(this.name);
      }
    }
    
    // Works, the 'name' property can be found on 'this'.
    const person = new Person("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.

    WDYT?

  3. self-assigned this
    on Nov 4, 2025
  4. added
    kind/bug
    on Nov 4, 2025
  5. added theissue type on Nov 4, 2025
  6. added a commit that references this issue on Nov 4, 2025
    11119ff
  7. jonkoops commented on Nov 4, 2025

    @jonkoops
    Contributor

    PR up under #204

  8. garronej commented on Nov 4, 2025

    @garronej

    Hey @jonkoops,

    I agree with the conclusion.

    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.

  9. jonkoops commented on Nov 4, 2025

    @jonkoops
    Contributor

    Nice, I merged the PR, there will likely be a couple more small fixes, after which we'll do a 26.2.2 release.

  10. added a commit that references this issue on Nov 4, 2025
    fd1fca4
  11. garronej commented on Nov 4, 2025

    @garronej

    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,

  12. marked logout() broken in 26.2.1 #210 as a duplicate of this issue on Nov 6, 2025
  13. mm-ruhri commented on Nov 24, 2025

    @mm-ruhri

    Thanks for fixing!
    Is there a chance, that the fix is released in near future?

  14. jonkoops commented on Nov 25, 2025

    @jonkoops
    Contributor

    Yes, I will hopefully get a release out the door this week, we're working on a couple more bugfixes before doing a release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Fields

No fields configured for bug.

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions