5.3 KiB · text History bbb2a48
defmodule VigilWeb.OrgInviteLive do
  @moduledoc """
  An organization invite link. What it offers depends on who opens it:

    * signed in as the invited address: Join;
    * signed in as someone else: a note (invites are addressed);
    * signed out, and the address has an account: log in (or NEIAM), then
      come back here;
    * signed out, no account: one click makes it (confirmed by the link)
      and joins; or NEIAM, which comes back here to finish
      (`VigilWeb.OrgInviteController`).
  """
  use VigilWeb, :live_view

  alias Vigil.{Accounts, Organizations}
  alias Vigil.Organizations.Roles

  @impl true
  def mount(%{"token" => token}, session, socket) do
    invite = Organizations.get_active_invite(token)
    user = socket.assigns.current_scope && socket.assigns.current_scope.user

    {:ok,
     socket
     |> assign(:page_title, "Invite")
     |> assign(:token, token)
     |> assign(:invite, invite)
     |> assign(:user, user)
     |> assign(:has_account?, !!invite && !user && !!Accounts.get_user_by_email(invite.email))
     |> assign(:sso_pending?, !!invite && session["vigil_org_invite_sso"] == token)}
  end

  @impl true
  def render(assigns) do
    ~H"""
    <Layouts.app flash={@flash} current_scope={@current_scope}>
      <div class="shader-glass mx-auto max-w-md space-y-6 py-6">
        <%= if is_nil(@invite) do %>
          <div id="invite-invalid" class="alert alert-warning">
            <.icon name="hero-exclamation-triangle" class="size-5" />
            <p>
              This invite link doesn't work: it was used, withdrawn, or has expired. Ask for a new one.
            </p>
          </div>
        <% else %>
          <div class="text-center">
            <span class="mx-auto mb-3 grid size-12 place-items-center rounded-xl bg-primary/10 text-xl font-bold text-primary">
              {@invite.organization.name |> String.first() |> String.upcase()}
            </span>
            <h1 class="text-2xl font-bold tracking-tight">Join {@invite.organization.name}</h1>
            <p class="text-sm text-base-content/70">
              As {Roles.label(@invite.role)}, for <span class="font-mono">{@invite.email}</span>.
            </p>
          </div>

          <%= cond do %>
            <% @user && @user.email == @invite.email -> %>
              <.form for={%{}} id="invite-join" action={~p"/invites/#{@token}/accept"}>
                <button type="submit" class="btn btn-primary w-full" phx-disable-with="Joining…">
                  Join {@invite.organization.name}
                </button>
              </.form>
            <% @user -> %>
              <div id="invite-wrong-account" class="alert alert-info text-sm">
                <p>
                  You're signed in as <span class="font-mono">{@user.email}</span>. Log out and open the
                  link again as <span class="font-mono">{@invite.email}</span>.
                </p>
              </div>
            <% @sso_pending? -> %>
              <.form
                for={%{}}
                id="invite-sso-accept"
                action={~p"/invites/#{@token}/sso"}
                class="space-y-3 rounded-box border border-primary/40 p-4"
              >
                <p class="text-sm">
                  You're signed in with NEIAM. This makes your Vigil account for
                  <span class="font-mono">{@invite.email}</span>
                  and joins {@invite.organization.name}.
                </p>
                <button type="submit" class="btn btn-primary w-full">Create my account</button>
              </.form>
            <% true -> %>
              <div class="space-y-3">
                <.link
                  :for={p <- NeiamAuth.providers()}
                  id={"invite-sso-#{p.id}"}
                  href={~p"/invites/#{@token}/sso?#{[provider: p.id]}"}
                  class="btn btn-primary w-full"
                >
                  {p.label}
                </.link>
                <div
                  :if={NeiamAuth.providers() != [] and NeiamAuth.local_login_enabled?()}
                  class="divider"
                >
                  or
                </div>
                <%= if NeiamAuth.local_login_enabled?() do %>
                  <%= if @has_account? do %>
                    <.link
                      id="invite-log-in"
                      href={~p"/invites/#{@token}/log-in"}
                      class="btn btn-primary btn-soft w-full"
                    >
                      Log in to accept
                    </.link>
                  <% else %>
                    <.form for={%{}} id="invite-new-account" action={~p"/invites/#{@token}/accept"}>
                      <p class="mb-2 text-sm text-base-content/70">
                        This makes an account for <span class="font-mono">{@invite.email}</span>
                        (the link counts as confirming it). You'll sign in later with a link we email you.
                      </p>
                      <button
                        type="submit"
                        class="btn btn-primary btn-soft w-full"
                        phx-disable-with="Creating…"
                      >
                        Create account and join
                      </button>
                    </.form>
                  <% end %>
                <% end %>
              </div>
          <% end %>
        <% end %>
      </div>
    </Layouts.app>
    """
  end
end