authenticationkeycloakkeycloak-rest-api

How to add user attributes to registration form in Keycloak?


I've created a new realm (out of the box and using user account for login/registration) in Keycloak and enabled the user profile, added a new attribute - shop

enter image description here

The user attribute enter image description here

Because this attribute is required, I want it to be in the default registration form

enter image description here

Is this possible to do?

It will be nice to have the user attributes in the JWT token as well


Solution

  • You can add a field to the registration form by adding a custom theme (You might do it by overriding the main theme, but they don't recommend overriding the main theme).

    Add a custom theme in the themes folder.

    Customize your ./themes/custom-theme/login/register.ftl file to add a custom field like the below: (Please follow the link)

    <div class="${properties.kcFormGroupClass!}">
                    <div class="${properties.kcLabelWrapperClass!}">
                        <label for="user.attributes.dob" class="${properties.kcLabelClass!}">
                        Date of birth</label>
                    </div>
    
                    <div class="${properties.kcInputWrapperClass!}">
                        <input type="date" class="${properties.kcInputClass!}" 
                        id="user.attributes.dob" name="user.attributes.dob" 
                        value="${(register.formData['user.attributes.dob']!'')}"/>
                    </div>
                </div>
    

    Then the dob will be added as user attributes after a successful registration.