docs: fix mistakes in the 'Confirmation' guide (#772)

This commit is contained in:
Sylvester 2024-08-17 17:44:14 +02:00 committed by GitHub
parent c9d12076ce
commit 36afc2b083
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -50,42 +50,86 @@ defmodule MyApp.Accounts.User do
confirm_on_create? true confirm_on_create? true
confirm_on_update? false confirm_on_update? false
confirm_action_name :confirm_new_user confirm_action_name :confirm_new_user
sender MyApp.NewUserConfirmationSender sender MyApp.Accounts.User.Senders.SendNewUserConfirmationEmail
end end
end end
end end
end end
``` ```
Next we will have to generate and run migrations to add confirmed_at column to user resource Next we will have to generate and run migrations to add confirmed_at column to user resource
```bash ```bash
mix ash.codegen account_confirmation mix ash.codegen account_confirmation
``` ```
Next we will define our "sender" module using `Swoosh`: To make this work we need to create a new module `MyApp.Accounts.User.Senders.SendPasswordResetEmail`:
```elixir ```elixir
defmodule MyApp.NewUserConfirmationSender do defmodule MyApp.Accounts.User.Senders.SendNewUserConfirmationEmail do
@moduledoc """
Sends an email confirmation email
"""
use AshAuthentication.Sender use AshAuthentication.Sender
use MyAppWeb, :verified_routes
@impl AshAuthentication.Sender
def send(user, token, _opts) do
MyApp.Accounts.Emails.deliver_email_confirmation_instructions(
user,
url(~p"/auth/user/confirm_new_user?#{[confirm: token]}")
)
end
end
```
We also need to create a new email template:
```elixir
defmodule Example.Accounts.Emails do
@moduledoc """
Delivers emails.
"""
import Swoosh.Email import Swoosh.Email
def send(user, confirm, _opts) do def deliver_email_confirmation_instructions(user, url) do
new() if !url do
|> to(to_string(user.email)) raise "Cannot deliver confirmation instructions without a url"
|> from({"MyApp Admin", "support@myapp.inc"}) end
|> subject("Confirm your email address")
|> html_body("""
<p>
Hi!<br />
Someone has tried to register a new account at <a href="https://myapp.inc">MyApp</a>. deliver(user.email, "Confirm your email address", """
If it was you, then please click the link below to confirm your identity. If you did not initiate this request then please ignore this email.
</p>
<p> <p>
<a href="https://myapp.inc/auth/user/confirm_new_user?#{URI.encode_query(confirm: confirm)}">Click here to confirm your account</a> Hi #{user.email},
</p>
<p>
Someone has tried to register a new account using this email address.
If it was you, then please click the link below to confirm your identity. If you did not initiate this request then please ignore this email.
</p>
<p>
<a href="#{url}">Click here to confirm your account</a>
</p> </p>
""") """)
|> MyApp.Mailer.deliver() end
# For simplicity, this module simply logs messages to the terminal.
# You should replace it by a proper email or notification tool, such as:
#
# * Swoosh - https://hexdocs.pm/swoosh
# * Bamboo - https://hexdocs.pm/bamboo
#
defp deliver(to, subject, body) do
IO.puts("Sending email to #{to} with subject #{subject} and body #{body}")
new()
|> from({"Zach", "zach@ash-hq.org"}) # TODO: Replace with your email
|> to(to_string(to))
|> subject(subject)
|> put_provider_option(:track_links, "None")
|> html_body(body)
|> MyApp.Mailer.deliver!()
end end
end end
``` ```
@ -111,7 +155,7 @@ defmodule MyApp.Accounts.User do
confirm_on_create? false confirm_on_create? false
confirm_on_update? true confirm_on_update? true
confirm_action_name :confirm_change confirm_action_name :confirm_change
sender MyApp.EmailChangeConfirmationSender sender MyApp.Accounts.User.Senders.SendEmailChangeConfirmationEmail
end end
end end
end end
@ -125,27 +169,50 @@ end
Next, let's define our new sender: Next, let's define our new sender:
```elixir ```elixir
defmodule MyApp.EmailChangeConfirmationSender do defmodule MyApp.Accounts.User.Senders.SendEmailChangeConfirmationEmail do
@moduledoc """
Sends an email change confirmation email
"""
use AshAuthentication.Sender use AshAuthentication.Sender
import Swoosh.Email use MyAppWeb, :verified_routes
@impl AshAuthentication.Sender
def send(user, token, _opts) do def send(user, token, _opts) do
new() MyApp.Accounts.Emails.deliver_email_change_confirmation_instructions(
|> to(to_string(user.email)) user,
|> from({"MyApp Admin", "support@myapp.inc"}) url(~p"/auth/user/confirm_change?#{[confirm: token]}")
|> subject("Confirm your new email address") )
|> html_body(""" end
<p> end
Hi!<br /> ```
You recently changed your email address on <a href="https://myapp.inc">MyApp</a>. Please confirm it. And our new email template:
</p>
```elixir
defmodule MyApp.Accounts.Emails do
# ...
def deliver_email_change_confirmation_instructions(user, url) do
if !url do
raise "Cannot deliver confirmation instructions without a url"
end
deliver(user.email, "Confirm your new email address", """
<p> <p>
<a href="https://myapp.inc/auth/user/confirm_change?#{URI.encode_query(token: token)}">Click here to confirm your new email address</a> Hi #{user.email},
</p>
<p>
You recently changed your email address. Please confirm it.
</p>
<p>
<a href="#{url}">Click here to confirm your new email address</a>
</p> </p>
""") """)
|> MyApp.Mailer.deliver()
end end
# ...
end end
``` ```