0

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.

Andre M
  • 6,649
  • 7
  • 52
  • 93

2 Answers2

1

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.

Andre M
  • 6,649
  • 7
  • 52
  • 93
  • Is there any idea for vue 3? I could not find any example of using loadUserInfo in conponent vue3 – hasanaydogar Nov 16 '21 at 19:08
  • I haven’t tried, since our project is still in Vue 2. I’d encourage anyone to provide a Vue 3 specific response. – Andre M Nov 17 '21 at 20:14
0

I think you can configure the 'Mappers' in your realms, and set the 'groups' type to 'Group Membership'.

like this: enter image description here