# Confirmation Tutorial This is a quick tutorial on how to configure your application to enable confirmation. In this tutorial we'll assume that you have a `User` resource which uses `email` as it's user identifier. We'll show you how to confirm a new user on sign-up and also require them to confirm if they wish to change their email address. Here's the user resource we'll be starting with: ```elixir defmodule MyApp.Accounts.User do use Ash.Resource, extensions: [AshAuthentication], domain: MyApp.Accounts attributes do uuid_primary_key :id attribute :email, :ci_string, allow_nil?: false, public?: true, sensitive?: true attribute :hashed_password, :string, allow_nil?: false, public?: false, sensitive?: true end authentication do strategies do password :password do identity_field :email hashed_password_field :hashed_password end end end identities do identity :unique_email, [:email] end end ``` ## Confirming newly registered users First we start by adding the confirmation add-on to your existing authentication DSL: ```elixir defmodule MyApp.Accounts.User do # ... authentication do # ... add_ons do confirmation :confirm_new_user do monitor_fields [:email] confirm_on_create? true confirm_on_update? false confirm_action_name :confirm_new_user sender MyApp.NewUserConfirmationSender end end end end ``` Next we will define our "sender" module using `Swoosh`: ```elixir defmodule MyApp.NewUserConfirmationSender do use AshAuthentication.Sender def send(user, token, _opts) do new() |> to(user.email) |> from({"MyApp Admin", "support@myapp.inc"}) |> subject("Confirm your email address") |> html_body("""
Hi!
Someone has tried to register a new account at MyApp.
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.
Click here to confirm your account
""") |> MyApp.Mailer.deliver() end end ``` Provided you have your authentication routes hooked up either via `AshAuthentication.Plug` or [`AshAuthentication.Phoenix.Router`](https://hexdocs.pm/ash_authentication_phoenix/AshAuthentication.Phoenix.Router.html) then the user will be confirmed when the token is submitted. ## Confirming changes to monitored fields You may want to require a user to perform a confirmation when a certain field changes. For example if a user changes their email address we can send them a new confirmation request. First, let's start by defining a new confirmation add-on in our resource: ```elixir defmodule MyApp.Accounts.User do # ... authentication do # ... add_ons do confirmation :confirm_change do monitor_fields [:email] confirm_on_create? false confirm_on_update? true confirm_action_name :confirm_change sender MyApp.EmailChangeConfirmationSender end end end end ``` > #### Why two confirmation configurations? {: .info} > > While you can perform both of these confirmations with a single confirmation add-on, in general the Ash philosophy is to be more explicit. Each confirmation will have it's own URL (based on the name) and tokens for one will not be able to be used for the other. Next, let's define our new sender: ```elixir defmodule MyApp.NewUserConfirmationSender do use AshAuthentication.Sender def send(user, token, _opts) do new() |> to(user.email) |> from({"MyApp Admin", "support@myapp.inc"}) |> subject("Confirm your new email address") |> html_body("""
Hi!
You recently changed your email address on MyApp. Please confirm it.