- Remove auto-confirmation on registration (register_user and register_user_with_organization) - Add deliver_user_confirmation_instructions/2 and confirm_user/1 to Accounts context - Add verify_email_token_query/2 and by_user_and_contexts_query/2 to UserToken - Make deliver_confirmation_email public in UserNotifier - Send confirmation email after registration in both flows - Block unconfirmed users at password login with helpful message - Add UserConfirmationController with confirm/resend routes - Add resend confirmation link to login page - Update test fixtures to confirm users after registration
33 lines
1.1 KiB
Elixir
33 lines
1.1 KiB
Elixir
defmodule ToweropsWeb.InvitationController do
|
|
use ToweropsWeb, :controller
|
|
|
|
alias Towerops.Organizations
|
|
|
|
def show(conn, %{"token" => token}) do
|
|
case Organizations.get_invitation_by_token(token) do
|
|
nil ->
|
|
conn
|
|
|> put_flash(:error, "This invitation is invalid or has expired.")
|
|
|> redirect(to: ~p"/")
|
|
|
|
invitation ->
|
|
case conn.assigns[:current_scope] do
|
|
%{user: user} when not is_nil(user) ->
|
|
case Organizations.accept_invitation(invitation, user.id) do
|
|
{:ok, _membership} ->
|
|
conn
|
|
|> put_flash(:info, "You've joined #{invitation.organization.name}!")
|
|
|> redirect(to: ~p"/orgs/#{invitation.organization.slug}/settings?tab=members")
|
|
|
|
{:error, _changeset} ->
|
|
conn
|
|
|> put_flash(:error, "Could not accept invitation. You may already be a member.")
|
|
|> redirect(to: ~p"/dashboard")
|
|
end
|
|
|
|
_ ->
|
|
redirect(conn, to: ~p"/users/register?invitation_token=#{token}")
|
|
end
|
|
end
|
|
end
|
|
end
|