Change user's name format

Hi. Currently user’s info has this interface:

{
    "sub": "xxxxx-xxxx-xxxx-xxxx-xxxxxxxx",
    "email_verified": true,
    "name": "Tan Nguyen",
    "preferred_username": "tan_nguyen",
    "given_name": "Tan",
    "family_name": "Nguyen",
    "email": "example@domain.com"
}

where name’s value equals {given_name} {family_name}. But in our country, user’s name should be {family_name} {given_name}. Is there a way to change user’s name format to {family_name} {given_name}?

Does anyone have any idea about this?

You could this by disabling the default name mapper in profile scope and add a custom javascript mapper according to the documentation:
https://www.keycloak.org/docs/latest/server_development/index.html#_script_providers
in the script file, a simple

result = user.lastName + " " + user.firstName;
result;

should be enough and return the desired value. Configure the mapper to write the returned value to the “name” claim of the userinfo in the mappers configuration.

1 Like