From 30df0726f1db3c95901c2bdc29b37a902f906008 Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Thu, 13 Jun 2024 16:36:26 +0200 Subject: [PATCH] Keycloak theme adjustments --- .../login/UserProfileFormFields.tsx | 114 ++++++++++++------ .../keycloak-theme/login/getKcContextMock.tsx | 3 + 2 files changed, 78 insertions(+), 39 deletions(-) diff --git a/web/src/keycloak-theme/login/UserProfileFormFields.tsx b/web/src/keycloak-theme/login/UserProfileFormFields.tsx index 571d7e4b9..8a3bf18e8 100644 --- a/web/src/keycloak-theme/login/UserProfileFormFields.tsx +++ b/web/src/keycloak-theme/login/UserProfileFormFields.tsx @@ -421,38 +421,72 @@ function CustomInputTag( } helperText={(() => { - { - const nonEmailPatternDisplayableErrors = - displayableErrors_props.filter( - ({ source }) => - !( - source.type === "validator" && - source.name === "pattern" && - attribute.name === "email" - ) - ); + allowed_email_domains: { + if (attribute.name !== "email") { + break allowed_email_domains; + } - if (nonEmailPatternDisplayableErrors.length !== 0) { - return nonEmailPatternDisplayableErrors.map( - ({ errorMessage }, i) => ( - {errorMessage}  - ) - ); + if (attribute.validators.pattern === undefined) { + break allowed_email_domains; } + + const isErrored = displayableErrors_props.length !== 0; + const hasPatternError = displayableErrors_props.some( + ({ source }) => + source.type === "validator" && source.name === "pattern" + ); + const hasCustomPatternErrorMessage = + attribute.validators.pattern["error-message"] !== undefined; + + if (isErrored && (!hasPatternError || hasCustomPatternErrorMessage)) { + break allowed_email_domains; + } + + return msg("allowed email domains"); } - { - // prettier-ignore - const { pattern } = attribute.validators; + username_pattern_message: { + if (attribute.name !== "username") { + break username_pattern_message; + } + + const isErrored = displayableErrors_props.length !== 0; + + if (isErrored) { + break username_pattern_message; + } + + if (attribute.validators.pattern === undefined) { + break username_pattern_message; + } + + const patternErrorMessage = + attribute.validators.pattern["error-message"]; + + if (patternErrorMessage === undefined) { + break username_pattern_message; + } - if (pattern !== undefined) { - const { "error-message": errorMessageKey } = pattern; + return advancedMsg(patternErrorMessage); + } - // prettier-ignore - return errorMessageKey !== undefined ? - advancedMsg(errorMessageKey) : - msg("must respect the pattern"); + error_messages_default_behavior: { + const isErrored = displayableErrors_props.length !== 0; + + if (!isErrored) { + break error_messages_default_behavior; } + + return ( + <> + {displayableErrors_props.map(({ errorMessage }, i, arr) => ( + + {errorMessage} + {arr.length - 1 !== i &&
} +
+ ))} + + ); } return undefined; @@ -461,22 +495,24 @@ function CustomInputTag( questionMarkHelperText={(() => { const { pattern } = attribute.validators.pattern ?? {}; - // prettier-ignore - return pattern === undefined ? - undefined : - attribute.name === "email" ? - (() => { + if (pattern === undefined) { + return undefined; + } - try { - return regExpStrToEmailDomains(pattern).join(", "); - } catch { - return pattern; - } + if (attribute.name === "email") { + + try { + return regExpStrToEmailDomains(pattern).join(", "); + } catch { + return pattern; + } + + } + + return displayableErrors_props.length === 0 ? + pattern : + undefined; - })() : - displayableErrors_props.length === 0 ? - pattern : - undefined; })()} doOnlyShowErrorAfterFirstFocusLost={false} // prettier-ignore diff --git a/web/src/keycloak-theme/login/getKcContextMock.tsx b/web/src/keycloak-theme/login/getKcContextMock.tsx index 59d5d2488..10fb90ba8 100644 --- a/web/src/keycloak-theme/login/getKcContextMock.tsx +++ b/web/src/keycloak-theme/login/getKcContextMock.tsx @@ -54,6 +54,9 @@ export const { getKcContextMock } = createGetKcContextMock({ "name": "email" } } + }, + "passwordPolicies": { + "length": 12 } } }