4

So I'm using keycloak as an authentication mechanism and customizing the login page by modifying the login.ftl file. So far so good. I'm importing the template.ftl file in my login.ftl - as instructed by keycloak's docs - so far so good. Within that template.ftl file - there's a way to access error messages as shown here

          <#if displayMessage && message?has_content && (message.type != 'warning' || !isAppInitiatedAction??)>
          <div class="alert alert-${message.type}">
              <#if message.type = 'success'><span class="${properties.kcFeedbackSuccessIcon!}"></span></#if>
              <#if message.type = 'warning'><span class="${properties.kcFeedbackWarningIcon!}"></span></#if>
              <#if message.type = 'error'><span class="${properties.kcFeedbackErrorIcon!}"></span></#if>
              <#if message.type = 'info'><span class="${properties.kcFeedbackInfoIcon!}"></span></#if>
              <span class="kc-feedback-text">${kcSanitize(message.summary)?no_esc}</span>
          </div>
      </#if>

Ok great! What if I don't want to handle the error message ui in that template.ftl file? I have a form UI on the login.ftl page that I'd like to display the error messages in. How can I either pass that message down to the login.ftl file OR access that error message from the login.ftl file? Thanks in advance for any guidance.

ChrisPalmer
  • 285
  • 4
  • 15

2 Answers2

6

You can access it in login.ftl just like in template.ftl

<#if message?has_content>
    message.summary
    ...
</#if>

I think you're having problem with template variables(displayMessage, isAppInitiatedAction) not being set in login.ftl

G K
  • 166
  • 4
1

If you are adding new fields in the login page and you need to validate it and show error message when the field is invalid you need to write a Provider. Custom provider must implement FormAction and FormActionFactory. There is method called validate, inside you can write your logic to validate the field and also define a key and value for the error message. This key can be used in your login.ftl file. This thread my be helpful Keycloak - Custom form action not visible in flow

sparrow
  • 1,825
  • 6
  • 24
  • 35