Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: call gettext fn on errors #573

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/ash_authentication_phoenix/components/magic_link.ex
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ defmodule AshAuthentication.Phoenix.Components.MagicLink do
id:
"#{subject_name}-#{Strategy.name(strategy)}-#{strategy.request_action_name}" |> slugify(),
tenant: socket.assigns.current_tenant,
transform_errors: _transform_errors(),
context:
Ash.Helpers.deep_merge_maps(context, %{
strategy: strategy,
Expand Down
26 changes: 14 additions & 12 deletions lib/ash_authentication_phoenix/components/password/register_form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,10 @@ defmodule AshAuthentication.Phoenix.Components.Password.RegisterForm do
domain = Info.authentication_domain!(strategy.resource)
subject_name = Info.authentication_subject_name!(strategy.resource)

form =
strategy.resource
|> Form.for_action(strategy.register_action_name,
domain: domain,
as: subject_name |> to_string(),
id:
"#{subject_name}-#{Strategy.name(strategy)}-#{strategy.register_action_name}"
|> slugify(),
context: %{strategy: strategy, private: %{ash_authentication?: true}}
)

socket =
socket
|> assign(assigns)
|> assign(
form: form,
trigger_action: false,
subject_name: subject_name
)
Expand All @@ -88,6 +76,20 @@ defmodule AshAuthentication.Phoenix.Components.Password.RegisterForm do
|> assign_new(:context, fn -> %{} end)
|> assign_new(:auth_routes_prefix, fn -> nil end)

form =
strategy.resource
|> Form.for_action(strategy.register_action_name,
domain: domain,
as: subject_name |> to_string(),
transform_errors: _transform_errors(),
id:
"#{subject_name}-#{Strategy.name(strategy)}-#{strategy.register_action_name}"
|> slugify(),
context: %{strategy: strategy, private: %{ash_authentication?: true}}
)

socket = assign(socket, :form, form)

{:ok, socket}
end

Expand Down
13 changes: 8 additions & 5 deletions lib/ash_authentication_phoenix/components/password/reset_form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ defmodule AshAuthentication.Phoenix.Components.Password.ResetForm do
@spec update(props, Socket.t()) :: {:ok, Socket.t()}
def update(assigns, socket) do
strategy = assigns.strategy
form = blank_form(strategy, assigns[:context] || %{})

socket =
socket
|> assign(assigns)
|> assign(form: form, subject_name: Info.authentication_subject_name!(strategy.resource))
|> assign(subject_name: Info.authentication_subject_name!(strategy.resource))
|> assign_new(:label, fn -> strategy.request_password_reset_action_name end)
|> assign_new(:inner_block, fn -> nil end)
|> assign_new(:overrides, fn -> [AshAuthentication.Phoenix.Overrides.Default] end)
Expand All @@ -72,7 +71,9 @@ defmodule AshAuthentication.Phoenix.Components.Password.ResetForm do
|> assign_new(:context, fn -> nil end)
|> assign_new(:auth_routes_prefix, fn -> nil end)

{:ok, socket}
form = blank_form(strategy, assigns[:context] || %{}, socket)

{:ok, assign(socket, form: form)}
end

@doc false
Expand Down Expand Up @@ -174,7 +175,7 @@ defmodule AshAuthentication.Phoenix.Components.Password.ResetForm do

socket =
socket
|> assign(:form, blank_form(strategy, socket.assigns[:context] || %{}))
|> assign(:form, blank_form(strategy, socket.assigns[:context] || %{}, socket))

socket =
if flash do
Expand All @@ -197,14 +198,16 @@ defmodule AshAuthentication.Phoenix.Components.Password.ResetForm do
Map.get(params, param_key, %{})
end

defp blank_form(%{resettable: resettable} = strategy, context) when not is_nil(resettable) do
defp blank_form(%{resettable: resettable} = strategy, context, socket)
when not is_nil(resettable) do
domain = Info.authentication_domain!(strategy.resource)
subject_name = Info.authentication_subject_name!(strategy.resource)

strategy.resource
|> Form.for_action(resettable.request_password_reset_action_name,
domain: domain,
as: subject_name |> to_string(),
transform_errors: _transform_errors(),
id:
"#{subject_name}-#{Strategy.name(strategy)}-#{resettable.request_password_reset_action_name}"
|> slugify(),
Expand Down
25 changes: 14 additions & 11 deletions lib/ash_authentication_phoenix/components/password/sign_in_form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ defmodule AshAuthentication.Phoenix.Components.Password.SignInForm do
domain = Info.authentication_domain!(strategy.resource)
subject_name = Info.authentication_subject_name!(strategy.resource)

socket =
socket
|> assign(assigns)
|> assign(trigger_action: false, subject_name: subject_name)
|> assign_new(:label, fn -> humanize(strategy.sign_in_action_name) end)
|> assign_new(:inner_block, fn -> nil end)
|> assign_new(:overrides, fn -> [AshAuthentication.Phoenix.Overrides.Default] end)
|> assign_new(:gettext_fn, fn -> nil end)
|> assign_new(:current_tenant, fn -> nil end)
|> assign_new(:context, fn -> %{} end)
|> assign_new(:auth_routes_prefix, fn -> nil end)

form =
strategy.resource
|> Form.for_action(strategy.sign_in_action_name,
Expand All @@ -71,24 +83,15 @@ defmodule AshAuthentication.Phoenix.Components.Password.SignInForm do
"#{subject_name}-#{Strategy.name(strategy)}-#{strategy.sign_in_action_name}"
|> slugify(),
tenant: assigns[:current_tenant],
transform_errors: _transform_errors(),
context:
Ash.Helpers.deep_merge_maps(assigns[:context] || %{}, %{
strategy: strategy,
private: %{ash_authentication?: true}
})
)

socket =
socket
|> assign(assigns)
|> assign(form: form, trigger_action: false, subject_name: subject_name)
|> assign_new(:label, fn -> humanize(strategy.sign_in_action_name) end)
|> assign_new(:inner_block, fn -> nil end)
|> assign_new(:overrides, fn -> [AshAuthentication.Phoenix.Overrides.Default] end)
|> assign_new(:gettext_fn, fn -> nil end)
|> assign_new(:current_tenant, fn -> nil end)
|> assign_new(:context, fn -> %{} end)
|> assign_new(:auth_routes_prefix, fn -> nil end)
socket = assign(socket, form: form, trigger_action: false, subject_name: subject_name)

{:ok, socket}
end
Expand Down
26 changes: 14 additions & 12 deletions lib/ash_authentication_phoenix/components/reset/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,10 @@ defmodule AshAuthentication.Phoenix.Components.Reset.Form do

resettable = strategy.resettable

form =
strategy.resource
|> Form.for_action(strategy.resettable.password_reset_action_name,
domain: domain,
as: subject_name |> to_string(),
id:
"#{subject_name}-#{Strategy.name(strategy)}-#{resettable.password_reset_action_name}"
|> slugify(),
context: %{strategy: strategy, private: %{ash_authentication?: true}}
)

socket =
socket
|> assign(assigns)
|> assign(
form: form,
trigger_action: false,
subject_name: subject_name,
resettable: resettable
Expand All @@ -89,6 +77,20 @@ defmodule AshAuthentication.Phoenix.Components.Reset.Form do
|> assign_new(:gettext_fn, fn -> nil end)
|> assign_new(:auth_routes_prefix, fn -> nil end)

form =
strategy.resource
|> Form.for_action(strategy.resettable.password_reset_action_name,
transform_errors: _transform_errors(),
domain: domain,
as: subject_name |> to_string(),
id:
"#{subject_name}-#{Strategy.name(strategy)}-#{resettable.password_reset_action_name}"
|> slugify(),
context: %{strategy: strategy, private: %{ash_authentication?: true}}
)

socket = assign(socket, form: form)

{:ok, socket}
end

Expand Down
19 changes: 19 additions & 0 deletions lib/ash_authentication_phoenix_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ defmodule AshAuthentication.Phoenix.Web do
Web.gettext_switch(unquote(gettext_fn), unquote(msgid), unquote(bindings))
end
end

defmacro _transform_errors do
quote do
alias AshPhoenix.FormData.Error

fn _source, error ->
if Error.impl_for(error) do
Error.to_form_error(error)
|> List.wrap()
# credo:disable-for-next-line Credo.Check.Refactor.Nesting
|> Enum.map(fn {field, message, vars} ->
{field, _gettext(message, vars), vars}
end)
else
error
end
end
end
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/mix/tasks/ash_authentication.phoenix.routes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Mix.Tasks.AshAuthentication.Phoenix.Routes do
Alternatively, you can modify your aliases task to run them back to back it.

```elixir
aliases: ["phx.routes": ["do", "phx.routes,", "ash_authentication.phx.routes"]]
aliases: ["phx.routes": ["do", "phx.routes,", "ash_authentication.phoenix.routes"]]
```
"""

Expand Down
Loading