defmodule CartosnapAppWeb.TripLive.Join do
@moduledoc """
Where an invite link lands (`/trips/join/<token>`): which trip it is,
who sent it, and the way in.
* Signed in: a Join button. Joining is a click rather than the visit
itself, so a link previewer or a prefetch can't spend the invite.
* Signed out, invite addressed to an address with no account: Join
makes one for that address (the link proves they hold it) and signs
them in (`CartosnapAppWeb.InviteController.join/2`).
* Signed out otherwise: log in or register, and come back here after.
Registering from here works while registration is closed: the
invite is the way in, and is spent on the new account.
* NEIAM: out through `InviteController.trip_sso/2`, so a first login
with no account (refused as not invited) comes back here, to accept
the invite with it (`InviteController.join_sso/2`).
"""
use CartosnapAppWeb, :live_view
alias CartosnapApp.Accounts
alias CartosnapApp.Trips
@impl true
def mount(%{"token" => token}, session, socket) do
scope = socket.assigns.current_scope
socket = assign(socket, :token, token)
case Trips.get_invite(token) do
{:ok, invite} ->
trip = invite.trip
signed_in? = scope != nil and scope.user != nil
{:ok,
socket
|> assign(:page_title, "Join #{trip.name}")
|> assign(:trip, trip)
|> assign(:invite, invite)
|> assign(:inviter, Accounts.display_name(invite.created_by))
|> assign(:count, trip |> Trips.list_album() |> length())
|> assign(:signed_in?, signed_in?)
|> assign(:already?, signed_in? and Trips.member?(scope, trip))
|> assign(:has_account?, invite.email && Accounts.get_user_by_email(invite.email) != nil)
# Set by NeiamAuthAdapter.handle_error/2: a NEIAM login from this
# invite came back refused as not invited, and is pending.
|> assign(:sso_pending?, session["cartosnap_trip_invite_sso"] == token)
|> assign(:sso_email, session["cartosnap_sso_email"])
|> assign(:problem, nil)}
{:error, :used} = error ->
# Used by this very user (registering from it, say): to the trip.
case Trips.trip_from_used_invite(scope, token) do
nil -> problem(socket, error)
trip -> {:ok, push_navigate(socket, to: ~p"/trips/#{trip}")}
end
error ->
problem(socket, error)
end
end
defp problem(socket, {:error, problem}) do
{:ok,
socket
|> assign(:page_title, "Invite")
|> assign(:trip, nil)
|> assign(:problem, problem)}
end
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash} current_scope={@current_scope} active={:trips}>
<div class="mx-auto max-w-md py-10">
<%= if @trip do %>
<div id="invite-card" class="card border border-base-300 bg-base-100 shadow-sm">
<div class="card-body items-center gap-3 text-center">
<span class="grid size-14 place-items-center rounded-2xl bg-primary/15 text-primary">
<.icon name="hero-paper-airplane" class="size-7" />
</span>
<p class="text-sm text-base-content/70">{@inviter} invited you to</p>
<h1 class="text-2xl font-bold tracking-tight">{@trip.name}</h1>
<p class="text-sm text-base-content/60">
{CartosnapAppWeb.TripLive.Index.date_range(@trip)} · {@count} {if @count == 1,
do: "photo",
else: "photos"} so far
</p>
<p class="text-sm">
Join to see everyone's photos from the trip on a map, and add your own to the album.
</p>
<%= cond do %>
<% @already? -> %>
<.link navigate={~p"/trips/#{@trip}"} class="btn btn-primary" id="open-trip">
You're already in. Open the trip
</.link>
<% @signed_in? -> %>
<button type="button" id="join-trip" phx-click="join" class="btn btn-primary">
Join {@trip.name}
</button>
<p
:if={@invite.email && @invite.email != String.downcase(@current_scope.user.email)}
class="text-xs text-base-content/60"
id="invite-addressed-elsewhere"
>
This invite was sent to {@invite.email}; you're signed in as {@current_scope.user.email}.
</p>
<% @sso_pending? -> %>
<%!-- Back from NEIAM with no account yet: accepting makes
one and links the login to it. --%>
<.form
for={%{}}
action={~p"/trips/join/#{@token}/sso"}
id="join-sso-accept"
class="w-full"
>
<button type="submit" class="btn btn-primary w-full" phx-disable-with="Joining…">
Make my account and join
</button>
</.form>
<p class="text-xs text-base-content/60">
You're signed in with NEIAM<span :if={@sso_email}> as {@sso_email}</span>. The account is for {@invite.email ||
@sso_email}, and signs in with NEIAM from now on.
</p>
<.link href={~p"/trips/join/#{@token}/log-in"} class="link text-sm" id="join-log-in">
I have an account already
</.link>
<% not NeiamAuth.local_login_enabled?() -> %>
<%!-- OIDC_LOGIN_MODE=sso_strict: in through NEIAM only,
then back here to join. --%>
<div id="join-sso" class="w-full">
<.sso_buttons token={@token} />
</div>
<p class="text-xs text-base-content/60">You'll come back here after.</p>
<% @invite.email && !@has_account? -> %>
<.form
for={%{}}
action={~p"/trips/join/#{@token}"}
id="join-new-account"
class="w-full"
>
<button type="submit" class="btn btn-primary w-full" phx-disable-with="Joining…">
Join as {@invite.email}
</button>
</.form>
<p class="text-xs text-base-content/60">
That makes you an account for {@invite.email}. You'll sign in later with a link we email you.
</p>
<.link href={~p"/trips/join/#{@token}/log-in"} class="link text-sm" id="join-log-in">
I have an account under another address
</.link>
<% true -> %>
<div class="flex w-full flex-col gap-2">
<div :if={NeiamAuth.enabled?() and NeiamAuth.providers() != []} id="join-sso">
<.sso_buttons token={@token} />
</div>
<.link
href={~p"/trips/join/#{@token}/log-in"}
class="btn btn-primary"
id="join-log-in"
>
Log in to join
</.link>
<.link
:if={!@has_account?}
href={~p"/trips/join/#{@token}/register"}
class="btn btn-ghost"
id="join-register"
>
Create an account
</.link>
</div>
<p class="text-xs text-base-content/60">You'll come back here after.</p>
<% end %>
</div>
</div>
<% else %>
<div id="invite-problem" class="rounded-box border border-base-300 p-8 text-center">
<.icon name="hero-link-slash" class="mx-auto size-10 text-base-content/40" />
<p class="mt-3 font-medium">{problem_text(@problem)}</p>
<p class="mt-1 text-sm text-base-content/60">Ask whoever sent it for a new one.</p>
</div>
<% end %>
</div>
</Layouts.app>
"""
end
attr :token, :string, required: true
# Through InviteController.trip_sso/2, which remembers the invite.
defp sso_buttons(assigns) do
~H"""
<.link
:for={p <- NeiamAuth.providers()}
href={~p"/trips/join/#{@token}/sso?#{[provider: p.id]}"}
class="btn btn-primary w-full"
>
{p.label}
</.link>
"""
end
defp problem_text(:used), do: "This invite has already been used."
defp problem_text(:expired), do: "This invite has expired."
defp problem_text(_), do: "This invite link doesn't work."
@impl true
def handle_event("join", _params, %{assigns: %{signed_in?: true}} = socket) do
case Trips.accept_invite(socket.assigns.current_scope, socket.assigns.token) do
{:ok, trip} ->
{:noreply,
socket
|> put_flash(:info, "Welcome to #{trip.name}! Add your photos to the album.")
|> push_navigate(to: ~p"/trips/#{trip}")}
{:error, problem} ->
{:noreply, socket |> assign(:trip, nil) |> assign(:problem, problem)}
end
end
def handle_event("join", _params, socket), do: {:noreply, socket}
end
neiam
/cartosnap_ex
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
9.3 KiB · text
History
e569b6d