This is a quick tutorial to get you up and running on Magic Links.
This assumes you've set up ash_authentication
and password reset in your Phoenix project.
# ...
strategies do
password :password do
identity_field(:email)
resettable do
sender(Example.Accounts.User.Senders.SendPasswordResetEmail)
end
end
# add these lines -->
magic_link do
identity_field :email
sender(Example.Accounts.User.Senders.SendMagicLink)
end
# <-- add these lines
end
# ...
Inside /lib/example/accounts/user/senders/send_magic_link.ex
defmodule Example.Accounts.User.Senders.SendMagicLink do
@moduledoc """
Sends a magic link
"""
use AshAuthentication.Sender
use ExampleWeb, :verified_routes
@impl AshAuthentication.Sender
def send(user, token, _) do
Example.Accounts.Emails.deliver_magic_link(
user,
url(~p"/auth/user/magic_link/?token=#{token}")
)
end
end
Inside /lib/example/accounts/emails.ex
# ...
def deliver_magic_link(user, url) do
if !url do
raise "Cannot deliver reset instructions without a url"
end
deliver(user.email, "Magic Link", """
<html>
<p>
Hi #{user.email},
</p>
<p>
<a href="#{url}">Click here</a> to login.
</p>
<html>
""")
end
# ...