Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keycloak FTL: how can we access error messages within the login template on a custom theme

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.

like image 811
ChrisPalmer Avatar asked Oct 29 '25 09:10

ChrisPalmer


2 Answers

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

like image 63
G K Avatar answered Nov 01 '25 06:11

G K


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

like image 35
sparrow Avatar answered Nov 01 '25 06:11

sparrow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!