|
1 | 1 | defmodule Craftplan.Accounts.User.Senders.SendNewUserConfirmationEmail do |
2 | 2 | @moduledoc """ |
3 | | - Sends an email for a new user to confirm their email address. |
| 3 | + Sends email confirmation or invited-account setup instructions for a new user. |
4 | 4 | """ |
5 | 5 |
|
6 | 6 | use AshAuthentication.Sender |
7 | 7 | use CraftplanWeb, :verified_routes |
8 | 8 |
|
| 9 | + alias AshAuthentication.Info |
| 10 | + alias AshAuthentication.Strategy.Password |
| 11 | + alias Craftplan.Accounts.Emails |
| 12 | + alias Craftplan.Accounts.User |
| 13 | + |
9 | 14 | @impl true |
10 | | - def send(user, token, _) do |
11 | | - Craftplan.Accounts.Emails.deliver_new_user_confirmation_email( |
| 15 | + def send(user, confirmation_token, opts) do |
| 16 | + send_for_action(user, confirmation_token, Keyword.get(opts, :changeset)) |
| 17 | + end |
| 18 | + |
| 19 | + defp send_for_action(user, _confirmation_token, %Ash.Changeset{action: %{name: :invite}}) do |
| 20 | + # Invited users start with an unknowable generated password. Setting a password through the |
| 21 | + # reset action also confirms the email, so they cannot be stranded after their first sign-out. |
| 22 | + password_strategy = Info.strategy!(User, :password) |
| 23 | + {:ok, reset_token} = Password.reset_token_for(password_strategy, user) |
| 24 | + |
| 25 | + Emails.deliver_invitation_email( |
| 26 | + user, |
| 27 | + url(~p"/password-reset/#{reset_token}") |
| 28 | + ) |
| 29 | + end |
| 30 | + |
| 31 | + defp send_for_action(user, confirmation_token, _changeset) do |
| 32 | + Emails.deliver_new_user_confirmation_email( |
12 | 33 | user, |
13 | | - url(~p"/auth/user/confirm_new_user?#{[confirm: token]}") |
| 34 | + url(~p"/auth/user/confirm_new_user?#{[confirm: confirmation_token]}") |
14 | 35 | ) |
15 | 36 | end |
16 | 37 | end |
0 commit comments