keycloak-angular
keycloak-angular copied to clipboard
Method to return the user attributes that can be customized at the Keycloak's Admin console at the "Clients -> Select a Client -> Mappers" tab
Bug Report or Feature Request (mark with an x
)
- [ ] bug report -> please search for issues before submitting
- [X] feature request
Versions.
keycloak-angular:3.0.2 angular:6.0.0 keycloak:3.4.3 provided by Red Hat SSO:7.2.2 (RH SSO)
Repro steps.
Not applicable
The log given by the failure.
Not applicable
Desired functionality.
I did not found a method to return the user attributes that can be customized at the Keycloak's Admin console at the "Clients -> Select a Client -> Mappers" tab. Only the basic attributes can be accessed using the KeycloakService.getKeycloakInstance().userInfo attributes.
To get the custom attribute, I parsed the JSON from the token and accessed the attribute manually, e.g:
this.phone = JSON.parse(atob(this.token.split(".")[1])).phone
Is this the best way to do it? Thanks for you help in advance.
@VSSILVA, for getting the custom user attributes you could get the attributes property from loadUserProfile method. For example:
let userDetails = await this.keycloakService.loadUserProfile();
console.log(this.userDetails.attributes.phone);
// another option with js destructuring
let {attributes} = await this.keycloakService.loadUserProfile();
Hello @mauriciovigolo.
I am getting a compilation error when I try to get de "attributes" property from the userDetails variable as you can see in the attached file. In which keycloak-angular version was this property available? I am using 3.0.2.
Thanks for your help.
@VSSILVA, sorry for the delay. Did you manage to deal with the custom userDetails attributes? The KeycloakProfile from keycloak-js definitions file doesn't have the attributes property.
I will add on v.4.1.0 and backward versions also. v.3.0.3. To solve this problem until the fix, you could use
this.userDetails['attributes'].phone
or even better:
let { phone } = this.userDetails['attributes'];
Thanks for reporting it!
@mauriciovigolo , Hey were you able to add attributes as one of the properties for KeycloakProfile? I tried to retrieve the attributes from loading the user profile but it says that "Property 'attributes' does not exist on type 'KeycloakProfile'."
@aniabraham I just faced same issue, managed to get attributes as suggested above:
this.keycloakService.loadUserProfile().then(profile => {
console.log(profile.username);
console.log(profile['attributes']); //gives you array of all attributes of user, extract what you need
})
[SOLVED] - I open the Token with a JWT client.
Hi, do you have other way to get user info? Other than through the endpoint account by method "loadUserProfile" /auth/realms/sfb-dev/account ?
I did not find in the library a method that accesses the userinfo endpoint. e.g: "http://$KC_SERVER/$KC_CONTEXT/realms/$KC_REALM/protocol/openid-connect/userinfo"
@yurisbv,
I was also trying to get id token and userinfo using this library. You can get them via the below method.
this.keycloakService.getKeycloakInstance()
- it returns the underlying keycloak js instance which has loadUserInfo() method that you can hit.