diff --git a/lib/ash_authentication/strategies/magic_link.ex b/lib/ash_authentication/strategies/magic_link.ex index aed869a..3641267 100644 --- a/lib/ash_authentication/strategies/magic_link.ex +++ b/lib/ash_authentication/strategies/magic_link.ex @@ -33,6 +33,8 @@ defmodule AshAuthentication.Strategy.MagicLink do sender fn user_or_email, token, _opts -> # will be a user if the token relates to an existing user # will be an email if there is no matching user (such as during sign up) + # opts will contain the `tenant` key, use this if you need to alter the link based + # on the tenant that requested the token MyApp.Emails.deliver_magic_link(user_or_email, token) end end @@ -45,6 +47,11 @@ defmodule AshAuthentication.Strategy.MagicLink do end ``` + ## Tenancy + + Note that the tenant is provided to the sender in the `opts` key. Use this if you need + to modify the url (i.e `tenant.app.com`) based on the tenant that requested the token. + ## Actions By default the magic link strategy will automatically generate the request and diff --git a/lib/ash_authentication/strategies/magic_link/request.ex b/lib/ash_authentication/strategies/magic_link/request.ex index c67c0ee..ca251e5 100644 --- a/lib/ash_authentication/strategies/magic_link/request.ex +++ b/lib/ash_authentication/strategies/magic_link/request.ex @@ -34,7 +34,7 @@ defmodule AshAuthentication.Strategy.MagicLink.Request do with true <- strategy.registration_enabled?, {sender, send_opts} <- strategy.sender, {:ok, token} <- MagicLink.request_token_for_identity(strategy, identity) do - sender.send(to_string(identity), token, send_opts) + sender.send(to_string(identity), token, Keyword.put(send_opts, :tenant, context.tenant)) else _ -> :ok @@ -43,7 +43,7 @@ defmodule AshAuthentication.Strategy.MagicLink.Request do {:ok, user} -> with {sender, send_opts} <- strategy.sender, {:ok, token} <- MagicLink.request_token_for_identity(strategy, identity) do - sender.send(user, token, send_opts) + sender.send(user, token, Keyword.put(send_opts, :tenant, context.tenant)) else _ -> :ok diff --git a/lib/ash_authentication/strategies/magic_link/request_preparation.ex b/lib/ash_authentication/strategies/magic_link/request_preparation.ex index 6df72dd..593c51b 100644 --- a/lib/ash_authentication/strategies/magic_link/request_preparation.ex +++ b/lib/ash_authentication/strategies/magic_link/request_preparation.ex @@ -19,7 +19,7 @@ defmodule AshAuthentication.Strategy.MagicLink.RequestPreparation do @doc false @impl true @spec prepare(Query.t(), keyword, Preparation.Context.t()) :: Query.t() - def prepare(query, _opts, _context) do + def prepare(query, _opts, context) do strategy = Info.strategy_for_action!(query.resource, query.action.name) identity_field = strategy.identity_field @@ -36,12 +36,12 @@ defmodule AshAuthentication.Strategy.MagicLink.RequestPreparation do |> Ash.Query.ensure_selected(select_for_senders) |> Ash.Query.ensure_selected([identity_field]) end) - |> Query.after_action(&after_action(&1, &2, strategy, identity)) + |> Query.after_action(&after_action(&1, &2, strategy, identity, context)) end - defp after_action(_query, [user], %{sender: {sender, send_opts}} = strategy, _identity) do + defp after_action(_query, [user], %{sender: {sender, send_opts}} = strategy, _identity, context) do case MagicLink.request_token_for(strategy, user) do - {:ok, token} -> sender.send(user, token, send_opts) + {:ok, token} -> sender.send(user, token, Keyword.put(send_opts, :tenant, context.tenant)) _ -> nil end @@ -52,11 +52,12 @@ defmodule AshAuthentication.Strategy.MagicLink.RequestPreparation do _query, _, %{registration_enabled?: true, sender: {sender, send_opts}} = strategy, - identity + identity, + context ) when not is_nil(identity) do case MagicLink.request_token_for_identity(strategy, identity) do - {:ok, token} -> sender.send(to_string(identity), token, send_opts) + {:ok, token} -> sender.send(to_string(identity), token, Keyword.put(send_opts, :tenant, context.tenant)) _ -> nil end