diff --git a/lib/ash_authentication_phoenix/components/magic_link.ex b/lib/ash_authentication_phoenix/components/magic_link.ex index 9400f00..0017d75 100644 --- a/lib/ash_authentication_phoenix/components/magic_link.ex +++ b/lib/ash_authentication_phoenix/components/magic_link.ex @@ -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, diff --git a/lib/ash_authentication_phoenix/components/password/register_form.ex b/lib/ash_authentication_phoenix/components/password/register_form.ex index 256d934..92e5c10 100644 --- a/lib/ash_authentication_phoenix/components/password/register_form.ex +++ b/lib/ash_authentication_phoenix/components/password/register_form.ex @@ -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 ) @@ -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 diff --git a/lib/ash_authentication_phoenix/components/password/reset_form.ex b/lib/ash_authentication_phoenix/components/password/reset_form.ex index 87108c7..065a0ae 100644 --- a/lib/ash_authentication_phoenix/components/password/reset_form.ex +++ b/lib/ash_authentication_phoenix/components/password/reset_form.ex @@ -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) @@ -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 @@ -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 @@ -197,7 +198,8 @@ 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) @@ -205,6 +207,7 @@ defmodule AshAuthentication.Phoenix.Components.Password.ResetForm do |> 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(), diff --git a/lib/ash_authentication_phoenix/components/password/sign_in_form.ex b/lib/ash_authentication_phoenix/components/password/sign_in_form.ex index d24c294..7eec224 100644 --- a/lib/ash_authentication_phoenix/components/password/sign_in_form.ex +++ b/lib/ash_authentication_phoenix/components/password/sign_in_form.ex @@ -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, @@ -71,6 +83,7 @@ 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, @@ -78,17 +91,7 @@ defmodule AshAuthentication.Phoenix.Components.Password.SignInForm do }) ) - 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 diff --git a/lib/ash_authentication_phoenix/components/reset/form.ex b/lib/ash_authentication_phoenix/components/reset/form.ex index 25e67bb..181657e 100644 --- a/lib/ash_authentication_phoenix/components/reset/form.ex +++ b/lib/ash_authentication_phoenix/components/reset/form.ex @@ -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 @@ -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 diff --git a/lib/ash_authentication_phoenix_web.ex b/lib/ash_authentication_phoenix_web.ex index a27ff5a..56b402a 100644 --- a/lib/ash_authentication_phoenix_web.ex +++ b/lib/ash_authentication_phoenix_web.ex @@ -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 diff --git a/lib/mix/tasks/ash_authentication.phoenix.routes.ex b/lib/mix/tasks/ash_authentication.phoenix.routes.ex index 8b6b8b3..b03ce0f 100644 --- a/lib/mix/tasks/ash_authentication.phoenix.routes.ex +++ b/lib/mix/tasks/ash_authentication.phoenix.routes.ex @@ -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"]] ``` """