defmodule VigilWeb.OrgInviteController do
@moduledoc """
The ways on from an organization invite (`/invites/:token`,
`VigilWeb.OrgInviteLive`) that need the session:
* `accept/2`: signed in, joins; signed out with no account for the
invite's address, makes that account (confirmed by the link), joins
and signs in;
* `log_in/2`: the address has an account: log in, then come back;
* `sso/2` and `accept_sso/2`: NEIAM, as for registration invites
(`VigilWeb.InviteController`). A first NEIAM login with no account is
refused as not invited; `Vigil.NeiamAuthAdapter.handle_error/2` sends
it back to the invite, where accepting makes the account for the
invite's address and links the pending login to it.
"""
use VigilWeb, :controller
alias Vigil.{Organizations, Repo}
alias VigilWeb.UserAuth
@org_invite :vigil_org_invite
@org_invite_sso :vigil_org_invite_sso
@doc false
def session_keys, do: %{org_invite: @org_invite, org_invite_sso: @org_invite_sso}
def accept(%{assigns: %{current_scope: %{user: %{} = user}}} = conn, %{"token" => token}) do
case Organizations.accept_invite(user, token) do
{:ok, {invite, _member}} ->
conn
|> put_flash(:info, "Welcome to #{invite.organization.name}!")
|> redirect(to: ~p"/orgs/#{invite.organization.slug}")
{:error, reason} ->
error(conn, token, reason)
end
end
def accept(conn, %{"token" => token}) do
if NeiamAuth.local_login_enabled?() do
case Organizations.accept_invite_new_account(token) do
{:ok, {invite, user}} ->
conn
|> NeiamAuth.link_pending(user)
|> welcome(invite)
|> UserAuth.log_in_user(user)
{:error, reason} ->
error(conn, token, reason)
end
else
conn
|> put_flash(:error, "Sign in with NEIAM to accept the invite.")
|> redirect(to: ~p"/invites/#{token}")
end
end
def log_in(conn, %{"token" => token}) do
conn
|> put_session(:user_return_to, ~p"/invites/#{token}")
|> redirect(to: ~p"/users/log-in")
end
# Out to NEIAM, remembering the invite: a login refused as not invited
# comes back to it (the adapter); one that succeeds returns to the invite
# page, signed in, to accept it.
def sso(conn, %{"token" => token, "provider" => id}) do
case NeiamAuth.config(id) do
{:ok, provider} ->
conn
|> put_session(@org_invite, token)
|> redirect(
to: NeiamAuth.Components.login_href(provider.id, ~p"/invites/#{token}", false)
)
_ ->
conn
|> put_flash(:error, "That sign-in isn't available.")
|> redirect(to: ~p"/invites/#{token}")
end
end
def accept_sso(conn, %{"token" => token}) do
result =
if get_session(conn, @org_invite_sso) == token do
Repo.transact(fn ->
with {:ok, {invite, user}} <- Organizations.accept_invite_new_account(token) do
conn = NeiamAuth.link_pending(conn, user)
if NeiamAuth.identities(user) == [],
do: {:error, :not_signed_in},
else: {:ok, {conn, invite, user}}
end
end)
else
{:error, :not_signed_in}
end
case result do
{:ok, {conn, invite, user}} ->
conn
|> delete_session(@org_invite)
|> delete_session(@org_invite_sso)
|> welcome(invite)
|> UserAuth.log_in_user(user)
{:error, reason} ->
error(conn, token, reason)
end
end
defp welcome(conn, invite) do
conn
|> put_session(:user_return_to, ~p"/orgs/#{invite.organization.slug}")
|> put_flash(:info, "Welcome to #{invite.organization.name}!")
end
defp error(conn, token, :has_account) do
conn
|> put_flash(:error, "That address already has an account. Log in to accept the invite.")
|> log_in(%{"token" => token})
end
defp error(conn, token, :wrong_account) do
conn
|> put_flash(
:error,
"This invite is for a different address than the account you're signed in with."
)
|> redirect(to: ~p"/invites/#{token}")
end
defp error(conn, token, :not_signed_in) do
conn
|> put_flash(:error, "Sign in with NEIAM first, then accept the invite.")
|> redirect(to: ~p"/invites/#{token}")
end
defp error(conn, token, _reason) do
conn
|> put_flash(:error, "That invite link doesn't work any more.")
|> redirect(to: ~p"/invites/#{token}")
end
end
neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions running · Badges · Packages
⭐
Log in to mark this repository.
4.4 KiB · text
History
bbb2a48