What’s your thoughts on pros/cons of design choices as it relates to how you could follow up with using the phx_gen_auth and at what entry points you could integrating with say Swoosh or Bamboo?
Let’s take the user’s registration controller for example.
lib/my_app_web/controllers/user_registration_controller.ex
alias MyAppWeb.{Mailer, AccountEmail}
def create(conn, %{"user" => user_params}) do
case Accounts.register_user(user_params) do
{:ok, user} ->
{:ok, _} =
Accounts.deliver_user_confirmation_instructions(
user,
&Routes.user_confirmation_url(conn, :confirm, &1),
&Mailer.deliver/1,
&AccountEmail.confirmation_email/1
)
...
end
end
You will notice that just as with the originally generated confirmation link helper, I’m also passing in the email and mailer’s deliver function to be used downstream.
Looking at how I choose to call said functions downstream in the context you will see
lib/my_app/accounts.ex
UserNotifier.deliver_instructions(
user,
confirmation_url_fun.(encoded_token),
deliver_fun,
email_fun
)
Using dependency injection I just pass the funs along to deliver_instructions
lib/my_app/accounts/user_notifier.ex
def deliver_instructions(user, url, deliver_fun, email_fun) do
Task.start(fn ->
%{url: url, email: user.email}
|> email_fun.()
|> deliver_fun.()
end)
end
In this example, I’m using swoosh and a very basic async deliver call.
Where I’ve hit the wall is the expected result of deliver_instructions
and trying to salvage many of the tests already generated by phx_gen_auth. The specific point of conflict looks to be the helper used for extracting the token.
def extract_user_token(fun) do
{:ok, captured} = fun.(&"[TOKEN]#{&1}[TOKEN]")
[_, token, _] = String.split(captured.body, "[TOKEN]")
token
end
This got me to question: if the result of deliver_instructions
is really only to benefit the test is it really the right choice? It’s likely it is, so with that what does deliver_instructions
need to return to satisfy the test?