mirror of
https://github.com/team-alembic/ash_authentication.git
synced 2024-09-21 13:53:25 +12:00
James Harton
bc1e590bca
* docs: Update README to new format. * docs: tidy up docus some more. * docs: More documentation tweaks. * docs: Tweak readme.
1.5 KiB
1.5 KiB
Magic Links Tutorial
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.
Add the Magic Link Strategy to the User resource
# ...
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
# ...
Create and email sender and email template
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
# ...