Skip to content

Commit

Permalink
improvement: provide tenant to the magic link sender as an opt
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Oct 20, 2024
1 parent 4390789 commit 2431efb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
7 changes: 7 additions & 0 deletions lib/ash_authentication/strategies/magic_link.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/ash_authentication/strategies/magic_link/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 2431efb

Please sign in to comment.