fix: honour the error field in AuthenticationFailed errors in forms. (#368)

When the field being validated is not the strategy's password field then don't show the normal validation message.
This commit is contained in:
James Harton 2024-02-07 09:08:36 +13:00 committed by GitHub
parent 2c3a12afa7
commit 82c4788962
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,7 +1,12 @@
defimpl AshPhoenix.FormData.Error, for: AshAuthentication.Errors.AuthenticationFailed do defimpl AshPhoenix.FormData.Error, for: AshAuthentication.Errors.AuthenticationFailed do
import PhoenixHTMLHelpers.Form, only: [humanize: 1] import PhoenixHTMLHelpers.Form, only: [humanize: 1]
def to_form_error(error) when is_struct(error.strategy, AshAuthentication.Strategy.Password) do def to_form_error(error) when is_struct(error.strategy, AshAuthentication.Strategy.Password),
do: to_auth_failed_error(error)
def to_form_error(_), do: []
defp to_auth_failed_error(error) when error.strategy.password_field == error.field do
[ [
{error.strategy.password_field, {error.strategy.password_field,
"#{humanize(error.strategy.identity_field)} or #{downcase_humanize(error.strategy.password_field)} was incorrect", "#{humanize(error.strategy.identity_field)} or #{downcase_humanize(error.strategy.password_field)} was incorrect",
@ -9,7 +14,11 @@ defimpl AshPhoenix.FormData.Error, for: AshAuthentication.Errors.AuthenticationF
] ]
end end
def to_form_error(_), do: [] defp to_auth_failed_error(error) do
[
{error.field, "#{humanize(error.field)} was incorrect", []}
]
end
defp downcase_humanize(value) do defp downcase_humanize(value) do
value value