Search code examples
vue.jskeycloakkeycloak-rest-apikeycloak-javascript

Listing the groups a user is a member of, using keycloak-js?


Our site is using keycloak for user auth, in a Vue app, with the user being added to a number of groups on the Keycloak server. The problem is that I am not sure how to discover the groups the user is a member of?

Looking at Vue.prototype.$keycloak does not reveal any functions for listing the groups and I see nothing in the documentation on how to do this either. I also checked the readme, but nothing documented there either. Lastly I tried looking in the REST documentation, as an alternative, but nothing that I could see that was relevant.

The application is a user facing Vue application, as opposed to an admin facing Vue application.

Using keycloak-js 12.0.3.


Solution

  • A bit of experimenting later it turns out it can be found via the loadUserInfo() function. In the context of my Vue app:

    const userInfo = Vue.prototype.$keycloak.loadUserInfo();
    

    Which yields an object of the form:

    {
        "sub": "57ac4e62-a4b0-4bc3-b43c-47d91deed19a",
        "email_verified": false,
        "name": "Test User",
        "groups": [
            "offline_access",
            "role-my-test-group",
            "uma_authorization"
        ],
        "preferred_username": "test-user",
        "given_name": "Test",
        "locale": "en",
        "family_name": "User"
    }
    

    In the above result we can see role-my-test-group role is present in the groups array. This is not the group that is being include, rather the role associated with the group - I initially assumed it was the groups being populated, when it reality it is the roles based on group membership.