Skip to content

Commit

Permalink
fix: ensure all forms set current_tenant
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Feb 17, 2025
1 parent a1fdf9a commit ad3aae0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 27 deletions.
18 changes: 2 additions & 16 deletions lib/ash_authentication_phoenix/components/password/reset_form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -154,22 +154,7 @@ defmodule AshAuthentication.Phoenix.Components.Password.ResetForm do

socket.assigns.form
|> Form.validate(params)
|> Form.submit(
params: params,
before_submit: fn
input when is_struct(input, Ash.ActionInput) ->
input
|> Ash.ActionInput.set_tenant(socket.assigns.current_tenant)

changeset when is_struct(changeset, Ash.Changeset) ->
changeset
|> Ash.Changeset.set_tenant(socket.assigns.current_tenant)

query when is_struct(query, Ash.Query) ->
query
|> Ash.Query.set_tenant(socket.assigns.current_tenant)
end
)
|> Form.submit(params: params)

flash = override_for(socket.assigns.overrides, :reset_flash_text)

Expand Down Expand Up @@ -208,6 +193,7 @@ defmodule AshAuthentication.Phoenix.Components.Password.ResetForm do
domain: domain,
as: subject_name |> to_string(),
transform_errors: _transform_errors(),
tenant: socket.assigns.current_tenant,
id:
"#{subject_name}-#{Strategy.name(strategy)}-#{resettable.request_password_reset_action_name}"
|> slugify(),
Expand Down
26 changes: 15 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 @@ -74,6 +74,19 @@ defmodule AshAuthentication.Phoenix.Components.Password.SignInForm do
|> assign_new(:context, fn -> %{} end)
|> assign_new(:auth_routes_prefix, fn -> nil end)

context =
Ash.Helpers.deep_merge_maps(assigns[:context] || %{}, %{
strategy: strategy,
private: %{ash_authentication?: true}
})

context =
if Map.get(socket.assigns.strategy, :sign_in_tokens_enabled?) do
Map.put(context, :token_type, :sign_in)
else
context
end

form =
strategy.resource
|> Form.for_action(strategy.sign_in_action_name,
Expand All @@ -84,11 +97,7 @@ defmodule AshAuthentication.Phoenix.Components.Password.SignInForm do
|> slugify(),
tenant: assigns[:current_tenant],
transform_errors: _transform_errors(),
context:
Ash.Helpers.deep_merge_maps(assigns[:context] || %{}, %{
strategy: strategy,
private: %{ash_authentication?: true}
})
context: context
)

socket = assign(socket, form: form, trigger_action: false, subject_name: subject_name)
Expand Down Expand Up @@ -173,12 +182,7 @@ defmodule AshAuthentication.Phoenix.Components.Password.SignInForm do
if Map.get(socket.assigns.strategy, :sign_in_tokens_enabled?) do
case Form.submit(socket.assigns.form,
params: params,
read_one?: true,
before_submit: fn changeset ->
changeset
|> Ash.Changeset.set_context(%{token_type: :sign_in})
|> Ash.Changeset.set_tenant(socket.assigns.current_tenant)
end
read_one?: true
) do
{:ok, user} ->
validate_sign_in_token_path =
Expand Down
3 changes: 3 additions & 0 deletions lib/ash_authentication_phoenix/components/reset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ defmodule AshAuthentication.Phoenix.Components.Reset do
@type props :: %{
required(:token) => String.t(),
optional(:overrides) => [module],
optional(:current_tenant) => term(),
optional(:gettext_fn) => {module, atom}
}

Expand All @@ -56,6 +57,7 @@ defmodule AshAuthentication.Phoenix.Components.Reset do
|> assign(strategies: strategies)
|> 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(:auth_routes_prefix, fn -> nil end)

{:ok, socket}
Expand All @@ -81,6 +83,7 @@ defmodule AshAuthentication.Phoenix.Components.Reset do
<.live_component
module={Components.Reset.Form}
auth_routes_prefix={@auth_routes_prefix}
current_tenant={@current_tenant}
strategy={strategy}
token={@token}
id={"#{strategy.name}-reset-form"}
Expand Down
3 changes: 3 additions & 0 deletions lib/ash_authentication_phoenix/components/reset/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ defmodule AshAuthentication.Phoenix.Components.Reset.Form do
optional(:label) => String.t() | false,
optional(:auth_routes_prefix) => String.t(),
optional(:overrides) => [module],
optional(:current_tenant) => term(),
optional(:gettext_fn) => {module, atom}
}

Expand All @@ -75,6 +76,7 @@ defmodule AshAuthentication.Phoenix.Components.Reset.Form do
|> assign_new(:label, fn -> humanize(resettable.password_reset_action_name) 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(:auth_routes_prefix, fn -> nil end)

form =
Expand All @@ -83,6 +85,7 @@ defmodule AshAuthentication.Phoenix.Components.Reset.Form do
transform_errors: _transform_errors(),
domain: domain,
as: subject_name |> to_string(),
tenant: socket.assigns.current_tenant,
id:
"#{subject_name}-#{Strategy.name(strategy)}-#{resettable.password_reset_action_name}"
|> slugify(),
Expand Down

0 comments on commit ad3aae0

Please sign in to comment.