Add all
This commit is contained in:
parent
b35e18cd20
commit
82f55f7afe
80 changed files with 6687 additions and 5 deletions
|
@ -12,6 +12,47 @@
|
|||
</script>
|
||||
</head>
|
||||
<body class="bg-white">
|
||||
<ul class="relative z-10 flex items-center gap-4 px-4 sm:px-6 lg:px-8 justify-end">
|
||||
<%= if @current_user do %>
|
||||
<li class="text-[0.8125rem] leading-6 text-zinc-900">
|
||||
{@current_user.email}
|
||||
</li>
|
||||
<li>
|
||||
<.link
|
||||
href={~p"/users/settings"}
|
||||
class="text-[0.8125rem] leading-6 text-zinc-900 font-semibold hover:text-zinc-700"
|
||||
>
|
||||
Settings
|
||||
</.link>
|
||||
</li>
|
||||
<li>
|
||||
<.link
|
||||
href={~p"/users/log_out"}
|
||||
method="delete"
|
||||
class="text-[0.8125rem] leading-6 text-zinc-900 font-semibold hover:text-zinc-700"
|
||||
>
|
||||
Log out
|
||||
</.link>
|
||||
</li>
|
||||
<% else %>
|
||||
<li>
|
||||
<.link
|
||||
href={~p"/users/register"}
|
||||
class="text-[0.8125rem] leading-6 text-zinc-900 font-semibold hover:text-zinc-700"
|
||||
>
|
||||
Register
|
||||
</.link>
|
||||
</li>
|
||||
<li>
|
||||
<.link
|
||||
href={~p"/users/log_in"}
|
||||
class="text-[0.8125rem] leading-6 text-zinc-900 font-semibold hover:text-zinc-700"
|
||||
>
|
||||
Log in
|
||||
</.link>
|
||||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
{@inner_content}
|
||||
</body>
|
||||
</html>
|
||||
|
|
43
lib/nulla_web/controllers/activity_controller.ex
Normal file
43
lib/nulla_web/controllers/activity_controller.ex
Normal file
|
@ -0,0 +1,43 @@
|
|||
defmodule NullaWeb.ActivityController do
|
||||
use NullaWeb, :controller
|
||||
|
||||
alias Nulla.Activities
|
||||
alias Nulla.Activities.Activity
|
||||
|
||||
action_fallback NullaWeb.FallbackController
|
||||
|
||||
def index(conn, _params) do
|
||||
activities = Activities.list_activities()
|
||||
render(conn, :index, activities: activities)
|
||||
end
|
||||
|
||||
def create(conn, %{"activity" => activity_params}) do
|
||||
with {:ok, %Activity{} = activity} <- Activities.create_activity(activity_params) do
|
||||
conn
|
||||
|> put_status(:created)
|
||||
|> put_resp_header("location", ~p"/api/activities/#{activity}")
|
||||
|> render(:show, activity: activity)
|
||||
end
|
||||
end
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
activity = Activities.get_activity!(id)
|
||||
render(conn, :show, activity: activity)
|
||||
end
|
||||
|
||||
def update(conn, %{"id" => id, "activity" => activity_params}) do
|
||||
activity = Activities.get_activity!(id)
|
||||
|
||||
with {:ok, %Activity{} = activity} <- Activities.update_activity(activity, activity_params) do
|
||||
render(conn, :show, activity: activity)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, %{"id" => id}) do
|
||||
activity = Activities.get_activity!(id)
|
||||
|
||||
with {:ok, %Activity{}} <- Activities.delete_activity(activity) do
|
||||
send_resp(conn, :no_content, "")
|
||||
end
|
||||
end
|
||||
end
|
41
lib/nulla_web/controllers/activity_json.ex
Normal file
41
lib/nulla_web/controllers/activity_json.ex
Normal file
|
@ -0,0 +1,41 @@
|
|||
defmodule NullaWeb.ActivityJSON do
|
||||
alias Nulla.Activities.Activity
|
||||
|
||||
@doc """
|
||||
Renders a list of activities.
|
||||
"""
|
||||
def index(%{activities: activities}) do
|
||||
%{data: for(activity <- activities, do: data(activity))}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Renders a single activity.
|
||||
"""
|
||||
def show(%{activity: activity}) do
|
||||
%{data: data(activity)}
|
||||
end
|
||||
|
||||
defp data(%Activity{} = activity) do
|
||||
%{
|
||||
id: activity.id,
|
||||
ap_id: activity.ap_id,
|
||||
type: activity.type,
|
||||
actor: activity.actor,
|
||||
object: activity.object,
|
||||
to: activity.to,
|
||||
cc: activity.cc
|
||||
}
|
||||
end
|
||||
|
||||
def activitypub(%Activity{} = activity) do
|
||||
Jason.OrderedObject.new(
|
||||
"@context": "https://www.w3.org/ns/activitystreams",
|
||||
id: activity.ap_id,
|
||||
type: activity.type,
|
||||
actor: activity.actor,
|
||||
object: activity.object,
|
||||
to: activity.to,
|
||||
cc: activity.cc
|
||||
)
|
||||
end
|
||||
end
|
43
lib/nulla_web/controllers/actor_controller.ex
Normal file
43
lib/nulla_web/controllers/actor_controller.ex
Normal file
|
@ -0,0 +1,43 @@
|
|||
defmodule NullaWeb.ActorController do
|
||||
use NullaWeb, :controller
|
||||
|
||||
alias Nulla.Actors
|
||||
alias Nulla.Actors.Actor
|
||||
|
||||
action_fallback NullaWeb.FallbackController
|
||||
|
||||
def index(conn, _params) do
|
||||
actors = Actors.list_actors()
|
||||
render(conn, :index, actors: actors)
|
||||
end
|
||||
|
||||
def create(conn, %{"actor" => actor_params}) do
|
||||
with {:ok, %Actor{} = actor} <- Actors.create_actor(actor_params) do
|
||||
conn
|
||||
|> put_status(:created)
|
||||
|> put_resp_header("location", ~p"/api/actors/#{actor}")
|
||||
|> render(:show, actor: actor)
|
||||
end
|
||||
end
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
actor = Actors.get_actor!(id)
|
||||
render(conn, :show, actor: actor)
|
||||
end
|
||||
|
||||
def update(conn, %{"id" => id, "actor" => actor_params}) do
|
||||
actor = Actors.get_actor!(id)
|
||||
|
||||
with {:ok, %Actor{} = actor} <- Actors.update_actor(actor, actor_params) do
|
||||
render(conn, :show, actor: actor)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, %{"id" => id}) do
|
||||
actor = Actors.get_actor!(id)
|
||||
|
||||
with {:ok, %Actor{}} <- Actors.delete_actor(actor) do
|
||||
send_resp(conn, :no_content, "")
|
||||
end
|
||||
end
|
||||
end
|
49
lib/nulla_web/controllers/actor_json.ex
Normal file
49
lib/nulla_web/controllers/actor_json.ex
Normal file
|
@ -0,0 +1,49 @@
|
|||
defmodule NullaWeb.ActorJSON do
|
||||
alias Nulla.Actors.Actor
|
||||
|
||||
@doc """
|
||||
Renders a list of actors.
|
||||
"""
|
||||
def index(%{actors: actors}) do
|
||||
%{data: for(actor <- actors, do: data(actor))}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Renders a single actor.
|
||||
"""
|
||||
def show(%{actor: actor}) do
|
||||
%{data: data(actor)}
|
||||
end
|
||||
|
||||
defp data(%Actor{} = actor) do
|
||||
%{
|
||||
id: actor.id,
|
||||
acct: actor.acct,
|
||||
ap_id: actor.ap_id,
|
||||
type: actor.type,
|
||||
following: actor.following,
|
||||
followers: actor.followers,
|
||||
inbox: actor.inbox,
|
||||
outbox: actor.outbox,
|
||||
featured: actor.featured,
|
||||
featuredTags: actor.featuredTags,
|
||||
preferredUsername: actor.preferredUsername,
|
||||
name: actor.name,
|
||||
summary: actor.summary,
|
||||
url: actor.url,
|
||||
manuallyApprovesFollowers: actor.manuallyApprovesFollowers,
|
||||
discoverable: actor.discoverable,
|
||||
indexable: actor.indexable,
|
||||
published: actor.published,
|
||||
memorial: actor.memorial,
|
||||
publicKey: actor.publicKey,
|
||||
tag: actor.tag,
|
||||
attachment: actor.attachment,
|
||||
endpoints: actor.endpoints,
|
||||
icon: actor.icon,
|
||||
image: actor.image,
|
||||
vcard_bday: actor.vcard_bday,
|
||||
vcard_Address: actor.vcard_Address
|
||||
}
|
||||
end
|
||||
end
|
25
lib/nulla_web/controllers/changeset_json.ex
Normal file
25
lib/nulla_web/controllers/changeset_json.ex
Normal file
|
@ -0,0 +1,25 @@
|
|||
defmodule NullaWeb.ChangesetJSON do
|
||||
@doc """
|
||||
Renders changeset errors.
|
||||
"""
|
||||
def error(%{changeset: changeset}) do
|
||||
# When encoded, the changeset returns its errors
|
||||
# as a JSON object. So we just pass it forward.
|
||||
%{errors: Ecto.Changeset.traverse_errors(changeset, &translate_error/1)}
|
||||
end
|
||||
|
||||
defp translate_error({msg, opts}) do
|
||||
# You can make use of gettext to translate error messages by
|
||||
# uncommenting and adjusting the following code:
|
||||
|
||||
# if count = opts[:count] do
|
||||
# Gettext.dngettext(NullaWeb.Gettext, "errors", msg, msg, count, opts)
|
||||
# else
|
||||
# Gettext.dgettext(NullaWeb.Gettext, "errors", msg, opts)
|
||||
# end
|
||||
|
||||
Enum.reduce(opts, msg, fn {key, value}, acc ->
|
||||
String.replace(acc, "%{#{key}}", fn _ -> to_string(value) end)
|
||||
end)
|
||||
end
|
||||
end
|
24
lib/nulla_web/controllers/fallback_controller.ex
Normal file
24
lib/nulla_web/controllers/fallback_controller.ex
Normal file
|
@ -0,0 +1,24 @@
|
|||
defmodule NullaWeb.FallbackController do
|
||||
@moduledoc """
|
||||
Translates controller action results into valid `Plug.Conn` responses.
|
||||
|
||||
See `Phoenix.Controller.action_fallback/1` for more details.
|
||||
"""
|
||||
use NullaWeb, :controller
|
||||
|
||||
# This clause handles errors returned by Ecto's insert/update/delete.
|
||||
def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
|
||||
conn
|
||||
|> put_status(:unprocessable_entity)
|
||||
|> put_view(json: NullaWeb.ChangesetJSON)
|
||||
|> render(:error, changeset: changeset)
|
||||
end
|
||||
|
||||
# This clause is an example of how to handle resources that cannot be found.
|
||||
def call(conn, {:error, :not_found}) do
|
||||
conn
|
||||
|> put_status(:not_found)
|
||||
|> put_view(html: NullaWeb.ErrorHTML, json: NullaWeb.ErrorJSON)
|
||||
|> render(:"404")
|
||||
end
|
||||
end
|
45
lib/nulla_web/controllers/media_attachment_controller.ex
Normal file
45
lib/nulla_web/controllers/media_attachment_controller.ex
Normal file
|
@ -0,0 +1,45 @@
|
|||
defmodule NullaWeb.MediaAttachmentController do
|
||||
use NullaWeb, :controller
|
||||
|
||||
alias Nulla.MediaAttachments
|
||||
alias Nulla.MediaAttachments.MediaAttachment
|
||||
|
||||
action_fallback NullaWeb.FallbackController
|
||||
|
||||
def index(conn, _params) do
|
||||
media_attachments = MediaAttachments.list_media_attachments()
|
||||
render(conn, :index, media_attachments: media_attachments)
|
||||
end
|
||||
|
||||
def create(conn, %{"media_attachment" => media_attachment_params}) do
|
||||
with {:ok, %MediaAttachment{} = media_attachment} <-
|
||||
MediaAttachments.create_media_attachment(media_attachment_params) do
|
||||
conn
|
||||
|> put_status(:created)
|
||||
|> put_resp_header("location", ~p"/api/media_attachments/#{media_attachment}")
|
||||
|> render(:show, media_attachment: media_attachment)
|
||||
end
|
||||
end
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
media_attachment = MediaAttachments.get_media_attachment!(id)
|
||||
render(conn, :show, media_attachment: media_attachment)
|
||||
end
|
||||
|
||||
def update(conn, %{"id" => id, "media_attachment" => media_attachment_params}) do
|
||||
media_attachment = MediaAttachments.get_media_attachment!(id)
|
||||
|
||||
with {:ok, %MediaAttachment{} = media_attachment} <-
|
||||
MediaAttachments.update_media_attachment(media_attachment, media_attachment_params) do
|
||||
render(conn, :show, media_attachment: media_attachment)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, %{"id" => id}) do
|
||||
media_attachment = MediaAttachments.get_media_attachment!(id)
|
||||
|
||||
with {:ok, %MediaAttachment{}} <- MediaAttachments.delete_media_attachment(media_attachment) do
|
||||
send_resp(conn, :no_content, "")
|
||||
end
|
||||
end
|
||||
end
|
29
lib/nulla_web/controllers/media_attachment_json.ex
Normal file
29
lib/nulla_web/controllers/media_attachment_json.ex
Normal file
|
@ -0,0 +1,29 @@
|
|||
defmodule NullaWeb.MediaAttachmentJSON do
|
||||
alias Nulla.MediaAttachments.MediaAttachment
|
||||
|
||||
@doc """
|
||||
Renders a list of media_attachments.
|
||||
"""
|
||||
def index(%{media_attachments: media_attachments}) do
|
||||
%{data: for(media_attachment <- media_attachments, do: data(media_attachment))}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Renders a single media_attachment.
|
||||
"""
|
||||
def show(%{media_attachment: media_attachment}) do
|
||||
%{data: data(media_attachment)}
|
||||
end
|
||||
|
||||
defp data(%MediaAttachment{} = media_attachment) do
|
||||
%{
|
||||
id: media_attachment.id,
|
||||
type: media_attachment.type,
|
||||
mediaType: media_attachment.mediaType,
|
||||
url: media_attachment.url,
|
||||
name: media_attachment.name,
|
||||
width: media_attachment.width,
|
||||
height: media_attachment.height
|
||||
}
|
||||
end
|
||||
end
|
43
lib/nulla_web/controllers/note_controller.ex
Normal file
43
lib/nulla_web/controllers/note_controller.ex
Normal file
|
@ -0,0 +1,43 @@
|
|||
defmodule NullaWeb.NoteController do
|
||||
use NullaWeb, :controller
|
||||
|
||||
alias Nulla.Notes
|
||||
alias Nulla.Notes.Note
|
||||
|
||||
action_fallback NullaWeb.FallbackController
|
||||
|
||||
def index(conn, _params) do
|
||||
notes = Notes.list_notes()
|
||||
render(conn, :index, notes: notes)
|
||||
end
|
||||
|
||||
def create(conn, %{"note" => note_params}) do
|
||||
with {:ok, %Note{} = note} <- Notes.create_note(note_params) do
|
||||
conn
|
||||
|> put_status(:created)
|
||||
|> put_resp_header("location", ~p"/api/notes/#{note}")
|
||||
|> render(:show, note: note)
|
||||
end
|
||||
end
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
note = Notes.get_note!(id)
|
||||
render(conn, :show, note: note)
|
||||
end
|
||||
|
||||
def update(conn, %{"id" => id, "note" => note_params}) do
|
||||
note = Notes.get_note!(id)
|
||||
|
||||
with {:ok, %Note{} = note} <- Notes.update_note(note, note_params) do
|
||||
render(conn, :show, note: note)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, %{"id" => id}) do
|
||||
note = Notes.get_note!(id)
|
||||
|
||||
with {:ok, %Note{}} <- Notes.delete_note(note) do
|
||||
send_resp(conn, :no_content, "")
|
||||
end
|
||||
end
|
||||
end
|
32
lib/nulla_web/controllers/note_json.ex
Normal file
32
lib/nulla_web/controllers/note_json.ex
Normal file
|
@ -0,0 +1,32 @@
|
|||
defmodule NullaWeb.NoteJSON do
|
||||
alias Nulla.Notes.Note
|
||||
|
||||
@doc """
|
||||
Renders a list of notes.
|
||||
"""
|
||||
def index(%{notes: notes}) do
|
||||
%{data: for(note <- notes, do: data(note))}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Renders a single note.
|
||||
"""
|
||||
def show(%{note: note}) do
|
||||
%{data: data(note)}
|
||||
end
|
||||
|
||||
defp data(%Note{} = note) do
|
||||
%{
|
||||
id: note.id,
|
||||
inReplyTo: note.inReplyTo,
|
||||
published: note.published,
|
||||
url: note.url,
|
||||
visibility: note.visibility,
|
||||
to: note.to,
|
||||
cc: note.cc,
|
||||
sensitive: note.sensitive,
|
||||
content: note.content,
|
||||
language: note.language
|
||||
}
|
||||
end
|
||||
end
|
43
lib/nulla_web/controllers/relation_controller.ex
Normal file
43
lib/nulla_web/controllers/relation_controller.ex
Normal file
|
@ -0,0 +1,43 @@
|
|||
defmodule NullaWeb.RelationController do
|
||||
use NullaWeb, :controller
|
||||
|
||||
alias Nulla.Relations
|
||||
alias Nulla.Relations.Relation
|
||||
|
||||
action_fallback NullaWeb.FallbackController
|
||||
|
||||
def index(conn, _params) do
|
||||
relations = Relations.list_relations()
|
||||
render(conn, :index, relations: relations)
|
||||
end
|
||||
|
||||
def create(conn, %{"relation" => relation_params}) do
|
||||
with {:ok, %Relation{} = relation} <- Relations.create_relation(relation_params) do
|
||||
conn
|
||||
|> put_status(:created)
|
||||
|> put_resp_header("location", ~p"/api/relations/#{relation}")
|
||||
|> render(:show, relation: relation)
|
||||
end
|
||||
end
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
relation = Relations.get_relation!(id)
|
||||
render(conn, :show, relation: relation)
|
||||
end
|
||||
|
||||
def update(conn, %{"id" => id, "relation" => relation_params}) do
|
||||
relation = Relations.get_relation!(id)
|
||||
|
||||
with {:ok, %Relation{} = relation} <- Relations.update_relation(relation, relation_params) do
|
||||
render(conn, :show, relation: relation)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, %{"id" => id}) do
|
||||
relation = Relations.get_relation!(id)
|
||||
|
||||
with {:ok, %Relation{}} <- Relations.delete_relation(relation) do
|
||||
send_resp(conn, :no_content, "")
|
||||
end
|
||||
end
|
||||
end
|
35
lib/nulla_web/controllers/relation_json.ex
Normal file
35
lib/nulla_web/controllers/relation_json.ex
Normal file
|
@ -0,0 +1,35 @@
|
|||
defmodule NullaWeb.RelationJSON do
|
||||
alias Nulla.Relations.Relation
|
||||
|
||||
@doc """
|
||||
Renders a list of relations.
|
||||
"""
|
||||
def index(%{relations: relations}) do
|
||||
%{data: for(relation <- relations, do: data(relation))}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Renders a single relation.
|
||||
"""
|
||||
def show(%{relation: relation}) do
|
||||
%{data: data(relation)}
|
||||
end
|
||||
|
||||
defp data(%Relation{} = relation) do
|
||||
%{
|
||||
id: relation.id,
|
||||
following: relation.following,
|
||||
followed_by: relation.followed_by,
|
||||
showing_replies: relation.showing_replies,
|
||||
showings_reblogs: relation.showings_reblogs,
|
||||
notifying: relation.notifying,
|
||||
muting: relation.muting,
|
||||
muting_notifications: relation.muting_notifications,
|
||||
blocking: relation.blocking,
|
||||
blocked_by: relation.blocked_by,
|
||||
domain_blocking: relation.domain_blocking,
|
||||
requested: relation.requested,
|
||||
note: relation.note
|
||||
}
|
||||
end
|
||||
end
|
42
lib/nulla_web/controllers/user_session_controller.ex
Normal file
42
lib/nulla_web/controllers/user_session_controller.ex
Normal file
|
@ -0,0 +1,42 @@
|
|||
defmodule NullaWeb.UserSessionController do
|
||||
use NullaWeb, :controller
|
||||
|
||||
alias Nulla.Accounts
|
||||
alias NullaWeb.UserAuth
|
||||
|
||||
def create(conn, %{"_action" => "registered"} = params) do
|
||||
create(conn, params, "Account created successfully!")
|
||||
end
|
||||
|
||||
def create(conn, %{"_action" => "password_updated"} = params) do
|
||||
conn
|
||||
|> put_session(:user_return_to, ~p"/users/settings")
|
||||
|> create(params, "Password updated successfully!")
|
||||
end
|
||||
|
||||
def create(conn, params) do
|
||||
create(conn, params, "Welcome back!")
|
||||
end
|
||||
|
||||
defp create(conn, %{"user" => user_params}, info) do
|
||||
%{"email" => email, "password" => password} = user_params
|
||||
|
||||
if user = Accounts.get_user_by_email_and_password(email, password) do
|
||||
conn
|
||||
|> put_flash(:info, info)
|
||||
|> UserAuth.log_in_user(user, user_params)
|
||||
else
|
||||
# In order to prevent user enumeration attacks, don't disclose whether the email is registered.
|
||||
conn
|
||||
|> put_flash(:error, "Invalid email or password")
|
||||
|> put_flash(:email, String.slice(email, 0, 160))
|
||||
|> redirect(to: ~p"/users/log_in")
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, _params) do
|
||||
conn
|
||||
|> put_flash(:info, "Logged out successfully.")
|
||||
|> UserAuth.log_out_user()
|
||||
end
|
||||
end
|
51
lib/nulla_web/live/user_confirmation_instructions_live.ex
Normal file
51
lib/nulla_web/live/user_confirmation_instructions_live.ex
Normal file
|
@ -0,0 +1,51 @@
|
|||
defmodule NullaWeb.UserConfirmationInstructionsLive do
|
||||
use NullaWeb, :live_view
|
||||
|
||||
alias Nulla.Accounts
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div class="mx-auto max-w-sm">
|
||||
<.header class="text-center">
|
||||
No confirmation instructions received?
|
||||
<:subtitle>We'll send a new confirmation link to your inbox</:subtitle>
|
||||
</.header>
|
||||
|
||||
<.simple_form for={@form} id="resend_confirmation_form" phx-submit="send_instructions">
|
||||
<.input field={@form[:email]} type="email" placeholder="Email" required />
|
||||
<:actions>
|
||||
<.button phx-disable-with="Sending..." class="w-full">
|
||||
Resend confirmation instructions
|
||||
</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
|
||||
<p class="text-center mt-4">
|
||||
<.link href={~p"/users/register"}>Register</.link>
|
||||
| <.link href={~p"/users/log_in"}>Log in</.link>
|
||||
</p>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, assign(socket, form: to_form(%{}, as: "user"))}
|
||||
end
|
||||
|
||||
def handle_event("send_instructions", %{"user" => %{"email" => email}}, socket) do
|
||||
if user = Accounts.get_user_by_email(email) do
|
||||
Accounts.deliver_user_confirmation_instructions(
|
||||
user,
|
||||
&url(~p"/users/confirm/#{&1}")
|
||||
)
|
||||
end
|
||||
|
||||
info =
|
||||
"If your email is in our system and it has not been confirmed yet, you will receive an email with instructions shortly."
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, info)
|
||||
|> redirect(to: ~p"/")}
|
||||
end
|
||||
end
|
58
lib/nulla_web/live/user_confirmation_live.ex
Normal file
58
lib/nulla_web/live/user_confirmation_live.ex
Normal file
|
@ -0,0 +1,58 @@
|
|||
defmodule NullaWeb.UserConfirmationLive do
|
||||
use NullaWeb, :live_view
|
||||
|
||||
alias Nulla.Accounts
|
||||
|
||||
def render(%{live_action: :edit} = assigns) do
|
||||
~H"""
|
||||
<div class="mx-auto max-w-sm">
|
||||
<.header class="text-center">Confirm Account</.header>
|
||||
|
||||
<.simple_form for={@form} id="confirmation_form" phx-submit="confirm_account">
|
||||
<input type="hidden" name={@form[:token].name} value={@form[:token].value} />
|
||||
<:actions>
|
||||
<.button phx-disable-with="Confirming..." class="w-full">Confirm my account</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
|
||||
<p class="text-center mt-4">
|
||||
<.link href={~p"/users/register"}>Register</.link>
|
||||
| <.link href={~p"/users/log_in"}>Log in</.link>
|
||||
</p>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def mount(%{"token" => token}, _session, socket) do
|
||||
form = to_form(%{"token" => token}, as: "user")
|
||||
{:ok, assign(socket, form: form), temporary_assigns: [form: nil]}
|
||||
end
|
||||
|
||||
# Do not log in the user after confirmation to avoid a
|
||||
# leaked token giving the user access to the account.
|
||||
def handle_event("confirm_account", %{"user" => %{"token" => token}}, socket) do
|
||||
case Accounts.confirm_user(token) do
|
||||
{:ok, _} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "User confirmed successfully.")
|
||||
|> redirect(to: ~p"/")}
|
||||
|
||||
:error ->
|
||||
# If there is a current user and the account was already confirmed,
|
||||
# then odds are that the confirmation link was already visited, either
|
||||
# by some automation or by the user themselves, so we redirect without
|
||||
# a warning message.
|
||||
case socket.assigns do
|
||||
%{current_user: %{confirmed_at: confirmed_at}} when not is_nil(confirmed_at) ->
|
||||
{:noreply, redirect(socket, to: ~p"/")}
|
||||
|
||||
%{} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:error, "User confirmation link is invalid or it has expired.")
|
||||
|> redirect(to: ~p"/")}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
50
lib/nulla_web/live/user_forgot_password_live.ex
Normal file
50
lib/nulla_web/live/user_forgot_password_live.ex
Normal file
|
@ -0,0 +1,50 @@
|
|||
defmodule NullaWeb.UserForgotPasswordLive do
|
||||
use NullaWeb, :live_view
|
||||
|
||||
alias Nulla.Accounts
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div class="mx-auto max-w-sm">
|
||||
<.header class="text-center">
|
||||
Forgot your password?
|
||||
<:subtitle>We'll send a password reset link to your inbox</:subtitle>
|
||||
</.header>
|
||||
|
||||
<.simple_form for={@form} id="reset_password_form" phx-submit="send_email">
|
||||
<.input field={@form[:email]} type="email" placeholder="Email" required />
|
||||
<:actions>
|
||||
<.button phx-disable-with="Sending..." class="w-full">
|
||||
Send password reset instructions
|
||||
</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
<p class="text-center text-sm mt-4">
|
||||
<.link href={~p"/users/register"}>Register</.link>
|
||||
| <.link href={~p"/users/log_in"}>Log in</.link>
|
||||
</p>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, assign(socket, form: to_form(%{}, as: "user"))}
|
||||
end
|
||||
|
||||
def handle_event("send_email", %{"user" => %{"email" => email}}, socket) do
|
||||
if user = Accounts.get_user_by_email(email) do
|
||||
Accounts.deliver_user_reset_password_instructions(
|
||||
user,
|
||||
&url(~p"/users/reset_password/#{&1}")
|
||||
)
|
||||
end
|
||||
|
||||
info =
|
||||
"If your email is in our system, you will receive instructions to reset your password shortly."
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, info)
|
||||
|> redirect(to: ~p"/")}
|
||||
end
|
||||
end
|
43
lib/nulla_web/live/user_login_live.ex
Normal file
43
lib/nulla_web/live/user_login_live.ex
Normal file
|
@ -0,0 +1,43 @@
|
|||
defmodule NullaWeb.UserLoginLive do
|
||||
use NullaWeb, :live_view
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div class="mx-auto max-w-sm">
|
||||
<.header class="text-center">
|
||||
Log in to account
|
||||
<:subtitle>
|
||||
Don't have an account?
|
||||
<.link navigate={~p"/users/register"} class="font-semibold text-brand hover:underline">
|
||||
Sign up
|
||||
</.link>
|
||||
for an account now.
|
||||
</:subtitle>
|
||||
</.header>
|
||||
|
||||
<.simple_form for={@form} id="login_form" action={~p"/users/log_in"} phx-update="ignore">
|
||||
<.input field={@form[:email]} type="email" label="Email" required />
|
||||
<.input field={@form[:password]} type="password" label="Password" required />
|
||||
|
||||
<:actions>
|
||||
<.input field={@form[:remember_me]} type="checkbox" label="Keep me logged in" />
|
||||
<.link href={~p"/users/reset_password"} class="text-sm font-semibold">
|
||||
Forgot your password?
|
||||
</.link>
|
||||
</:actions>
|
||||
<:actions>
|
||||
<.button phx-disable-with="Logging in..." class="w-full">
|
||||
Log in <span aria-hidden="true">→</span>
|
||||
</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def mount(_params, _session, socket) do
|
||||
email = Phoenix.Flash.get(socket.assigns.flash, :email)
|
||||
form = to_form(%{"email" => email}, as: "user")
|
||||
{:ok, assign(socket, form: form), temporary_assigns: [form: form]}
|
||||
end
|
||||
end
|
87
lib/nulla_web/live/user_registration_live.ex
Normal file
87
lib/nulla_web/live/user_registration_live.ex
Normal file
|
@ -0,0 +1,87 @@
|
|||
defmodule NullaWeb.UserRegistrationLive do
|
||||
use NullaWeb, :live_view
|
||||
|
||||
alias Nulla.Accounts
|
||||
alias Nulla.Accounts.User
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div class="mx-auto max-w-sm">
|
||||
<.header class="text-center">
|
||||
Register for an account
|
||||
<:subtitle>
|
||||
Already registered?
|
||||
<.link navigate={~p"/users/log_in"} class="font-semibold text-brand hover:underline">
|
||||
Log in
|
||||
</.link>
|
||||
to your account now.
|
||||
</:subtitle>
|
||||
</.header>
|
||||
|
||||
<.simple_form
|
||||
for={@form}
|
||||
id="registration_form"
|
||||
phx-submit="save"
|
||||
phx-change="validate"
|
||||
phx-trigger-action={@trigger_submit}
|
||||
action={~p"/users/log_in?_action=registered"}
|
||||
method="post"
|
||||
>
|
||||
<.error :if={@check_errors}>
|
||||
Oops, something went wrong! Please check the errors below.
|
||||
</.error>
|
||||
|
||||
<.input field={@form[:email]} type="email" label="Email" required />
|
||||
<.input field={@form[:password]} type="password" label="Password" required />
|
||||
|
||||
<:actions>
|
||||
<.button phx-disable-with="Creating account..." class="w-full">Create an account</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def mount(_params, _session, socket) do
|
||||
changeset = Accounts.change_user_registration(%User{})
|
||||
|
||||
socket =
|
||||
socket
|
||||
|> assign(trigger_submit: false, check_errors: false)
|
||||
|> assign_form(changeset)
|
||||
|
||||
{:ok, socket, temporary_assigns: [form: nil]}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"user" => user_params}, socket) do
|
||||
case Accounts.register_user(user_params) do
|
||||
{:ok, user} ->
|
||||
{:ok, _} =
|
||||
Accounts.deliver_user_confirmation_instructions(
|
||||
user,
|
||||
&url(~p"/users/confirm/#{&1}")
|
||||
)
|
||||
|
||||
changeset = Accounts.change_user_registration(user)
|
||||
{:noreply, socket |> assign(trigger_submit: true) |> assign_form(changeset)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, socket |> assign(check_errors: true) |> assign_form(changeset)}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("validate", %{"user" => user_params}, socket) do
|
||||
changeset = Accounts.change_user_registration(%User{}, user_params)
|
||||
{:noreply, assign_form(socket, Map.put(changeset, :action, :validate))}
|
||||
end
|
||||
|
||||
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
|
||||
form = to_form(changeset, as: "user")
|
||||
|
||||
if changeset.valid? do
|
||||
assign(socket, form: form, check_errors: false)
|
||||
else
|
||||
assign(socket, form: form)
|
||||
end
|
||||
end
|
||||
end
|
89
lib/nulla_web/live/user_reset_password_live.ex
Normal file
89
lib/nulla_web/live/user_reset_password_live.ex
Normal file
|
@ -0,0 +1,89 @@
|
|||
defmodule NullaWeb.UserResetPasswordLive do
|
||||
use NullaWeb, :live_view
|
||||
|
||||
alias Nulla.Accounts
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div class="mx-auto max-w-sm">
|
||||
<.header class="text-center">Reset Password</.header>
|
||||
|
||||
<.simple_form
|
||||
for={@form}
|
||||
id="reset_password_form"
|
||||
phx-submit="reset_password"
|
||||
phx-change="validate"
|
||||
>
|
||||
<.error :if={@form.errors != []}>
|
||||
Oops, something went wrong! Please check the errors below.
|
||||
</.error>
|
||||
|
||||
<.input field={@form[:password]} type="password" label="New password" required />
|
||||
<.input
|
||||
field={@form[:password_confirmation]}
|
||||
type="password"
|
||||
label="Confirm new password"
|
||||
required
|
||||
/>
|
||||
<:actions>
|
||||
<.button phx-disable-with="Resetting..." class="w-full">Reset Password</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
|
||||
<p class="text-center text-sm mt-4">
|
||||
<.link href={~p"/users/register"}>Register</.link>
|
||||
| <.link href={~p"/users/log_in"}>Log in</.link>
|
||||
</p>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def mount(params, _session, socket) do
|
||||
socket = assign_user_and_token(socket, params)
|
||||
|
||||
form_source =
|
||||
case socket.assigns do
|
||||
%{user: user} ->
|
||||
Accounts.change_user_password(user)
|
||||
|
||||
_ ->
|
||||
%{}
|
||||
end
|
||||
|
||||
{:ok, assign_form(socket, form_source), temporary_assigns: [form: nil]}
|
||||
end
|
||||
|
||||
# Do not log in the user after reset password to avoid a
|
||||
# leaked token giving the user access to the account.
|
||||
def handle_event("reset_password", %{"user" => user_params}, socket) do
|
||||
case Accounts.reset_user_password(socket.assigns.user, user_params) do
|
||||
{:ok, _} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Password reset successfully.")
|
||||
|> redirect(to: ~p"/users/log_in")}
|
||||
|
||||
{:error, changeset} ->
|
||||
{:noreply, assign_form(socket, Map.put(changeset, :action, :insert))}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("validate", %{"user" => user_params}, socket) do
|
||||
changeset = Accounts.change_user_password(socket.assigns.user, user_params)
|
||||
{:noreply, assign_form(socket, Map.put(changeset, :action, :validate))}
|
||||
end
|
||||
|
||||
defp assign_user_and_token(socket, %{"token" => token}) do
|
||||
if user = Accounts.get_user_by_reset_password_token(token) do
|
||||
assign(socket, user: user, token: token)
|
||||
else
|
||||
socket
|
||||
|> put_flash(:error, "Reset password link is invalid or it has expired.")
|
||||
|> redirect(to: ~p"/")
|
||||
end
|
||||
end
|
||||
|
||||
defp assign_form(socket, %{} = source) do
|
||||
assign(socket, :form, to_form(source, as: "user"))
|
||||
end
|
||||
end
|
167
lib/nulla_web/live/user_settings_live.ex
Normal file
167
lib/nulla_web/live/user_settings_live.ex
Normal file
|
@ -0,0 +1,167 @@
|
|||
defmodule NullaWeb.UserSettingsLive do
|
||||
use NullaWeb, :live_view
|
||||
|
||||
alias Nulla.Accounts
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<.header class="text-center">
|
||||
Account Settings
|
||||
<:subtitle>Manage your account email address and password settings</:subtitle>
|
||||
</.header>
|
||||
|
||||
<div class="space-y-12 divide-y">
|
||||
<div>
|
||||
<.simple_form
|
||||
for={@email_form}
|
||||
id="email_form"
|
||||
phx-submit="update_email"
|
||||
phx-change="validate_email"
|
||||
>
|
||||
<.input field={@email_form[:email]} type="email" label="Email" required />
|
||||
<.input
|
||||
field={@email_form[:current_password]}
|
||||
name="current_password"
|
||||
id="current_password_for_email"
|
||||
type="password"
|
||||
label="Current password"
|
||||
value={@email_form_current_password}
|
||||
required
|
||||
/>
|
||||
<:actions>
|
||||
<.button phx-disable-with="Changing...">Change Email</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
</div>
|
||||
<div>
|
||||
<.simple_form
|
||||
for={@password_form}
|
||||
id="password_form"
|
||||
action={~p"/users/log_in?_action=password_updated"}
|
||||
method="post"
|
||||
phx-change="validate_password"
|
||||
phx-submit="update_password"
|
||||
phx-trigger-action={@trigger_submit}
|
||||
>
|
||||
<input
|
||||
name={@password_form[:email].name}
|
||||
type="hidden"
|
||||
id="hidden_user_email"
|
||||
value={@current_email}
|
||||
/>
|
||||
<.input field={@password_form[:password]} type="password" label="New password" required />
|
||||
<.input
|
||||
field={@password_form[:password_confirmation]}
|
||||
type="password"
|
||||
label="Confirm new password"
|
||||
/>
|
||||
<.input
|
||||
field={@password_form[:current_password]}
|
||||
name="current_password"
|
||||
type="password"
|
||||
label="Current password"
|
||||
id="current_password_for_password"
|
||||
value={@current_password}
|
||||
required
|
||||
/>
|
||||
<:actions>
|
||||
<.button phx-disable-with="Changing...">Change Password</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def mount(%{"token" => token}, _session, socket) do
|
||||
socket =
|
||||
case Accounts.update_user_email(socket.assigns.current_user, token) do
|
||||
:ok ->
|
||||
put_flash(socket, :info, "Email changed successfully.")
|
||||
|
||||
:error ->
|
||||
put_flash(socket, :error, "Email change link is invalid or it has expired.")
|
||||
end
|
||||
|
||||
{:ok, push_navigate(socket, to: ~p"/users/settings")}
|
||||
end
|
||||
|
||||
def mount(_params, _session, socket) do
|
||||
user = socket.assigns.current_user
|
||||
email_changeset = Accounts.change_user_email(user)
|
||||
password_changeset = Accounts.change_user_password(user)
|
||||
|
||||
socket =
|
||||
socket
|
||||
|> assign(:current_password, nil)
|
||||
|> assign(:email_form_current_password, nil)
|
||||
|> assign(:current_email, user.email)
|
||||
|> assign(:email_form, to_form(email_changeset))
|
||||
|> assign(:password_form, to_form(password_changeset))
|
||||
|> assign(:trigger_submit, false)
|
||||
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
def handle_event("validate_email", params, socket) do
|
||||
%{"current_password" => password, "user" => user_params} = params
|
||||
|
||||
email_form =
|
||||
socket.assigns.current_user
|
||||
|> Accounts.change_user_email(user_params)
|
||||
|> Map.put(:action, :validate)
|
||||
|> to_form()
|
||||
|
||||
{:noreply, assign(socket, email_form: email_form, email_form_current_password: password)}
|
||||
end
|
||||
|
||||
def handle_event("update_email", params, socket) do
|
||||
%{"current_password" => password, "user" => user_params} = params
|
||||
user = socket.assigns.current_user
|
||||
|
||||
case Accounts.apply_user_email(user, password, user_params) do
|
||||
{:ok, applied_user} ->
|
||||
Accounts.deliver_user_update_email_instructions(
|
||||
applied_user,
|
||||
user.email,
|
||||
&url(~p"/users/settings/confirm_email/#{&1}")
|
||||
)
|
||||
|
||||
info = "A link to confirm your email change has been sent to the new address."
|
||||
{:noreply, socket |> put_flash(:info, info) |> assign(email_form_current_password: nil)}
|
||||
|
||||
{:error, changeset} ->
|
||||
{:noreply, assign(socket, :email_form, to_form(Map.put(changeset, :action, :insert)))}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("validate_password", params, socket) do
|
||||
%{"current_password" => password, "user" => user_params} = params
|
||||
|
||||
password_form =
|
||||
socket.assigns.current_user
|
||||
|> Accounts.change_user_password(user_params)
|
||||
|> Map.put(:action, :validate)
|
||||
|> to_form()
|
||||
|
||||
{:noreply, assign(socket, password_form: password_form, current_password: password)}
|
||||
end
|
||||
|
||||
def handle_event("update_password", params, socket) do
|
||||
%{"current_password" => password, "user" => user_params} = params
|
||||
user = socket.assigns.current_user
|
||||
|
||||
case Accounts.update_user_password(user, password, user_params) do
|
||||
{:ok, user} ->
|
||||
password_form =
|
||||
user
|
||||
|> Accounts.change_user_password(user_params)
|
||||
|> to_form()
|
||||
|
||||
{:noreply, assign(socket, trigger_submit: true, password_form: password_form)}
|
||||
|
||||
{:error, changeset} ->
|
||||
{:noreply, assign(socket, password_form: to_form(changeset))}
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,6 +1,8 @@
|
|||
defmodule NullaWeb.Router do
|
||||
use NullaWeb, :router
|
||||
|
||||
import NullaWeb.UserAuth
|
||||
|
||||
pipeline :browser do
|
||||
plug :accepts, ["html"]
|
||||
plug :fetch_session
|
||||
|
@ -8,10 +10,12 @@ defmodule NullaWeb.Router do
|
|||
plug :put_root_layout, html: {NullaWeb.Layouts, :root}
|
||||
plug :protect_from_forgery
|
||||
plug :put_secure_browser_headers
|
||||
plug :fetch_current_user
|
||||
end
|
||||
|
||||
pipeline :api do
|
||||
plug :accepts, ["json"]
|
||||
plug :fetch_api_user
|
||||
end
|
||||
|
||||
scope "/", NullaWeb do
|
||||
|
@ -20,10 +24,15 @@ defmodule NullaWeb.Router do
|
|||
get "/", PageController, :home
|
||||
end
|
||||
|
||||
# Other scopes may use custom stacks.
|
||||
# scope "/api", NullaWeb do
|
||||
# pipe_through :api
|
||||
# end
|
||||
scope "/api", NullaWeb do
|
||||
pipe_through :api
|
||||
|
||||
resources "/actors", ActorController, except: [:new, :edit]
|
||||
resources "/notes", NoteController, except: [:new, :edit]
|
||||
resources "/media_attachments", MediaAttachmentController, except: [:new, :edit]
|
||||
resources "/relations", RelationController, except: [:new, :edit]
|
||||
resources "/activities", ActivityController, except: [:new, :edit]
|
||||
end
|
||||
|
||||
# Enable LiveDashboard and Swoosh mailbox preview in development
|
||||
if Application.compile_env(:nulla, :dev_routes) do
|
||||
|
@ -41,4 +50,42 @@ defmodule NullaWeb.Router do
|
|||
forward "/mailbox", Plug.Swoosh.MailboxPreview
|
||||
end
|
||||
end
|
||||
|
||||
## Authentication routes
|
||||
|
||||
scope "/", NullaWeb do
|
||||
pipe_through [:browser, :redirect_if_user_is_authenticated]
|
||||
|
||||
live_session :redirect_if_user_is_authenticated,
|
||||
on_mount: [{NullaWeb.UserAuth, :redirect_if_user_is_authenticated}] do
|
||||
live "/users/register", UserRegistrationLive, :new
|
||||
live "/users/log_in", UserLoginLive, :new
|
||||
live "/users/reset_password", UserForgotPasswordLive, :new
|
||||
live "/users/reset_password/:token", UserResetPasswordLive, :edit
|
||||
end
|
||||
|
||||
post "/users/log_in", UserSessionController, :create
|
||||
end
|
||||
|
||||
scope "/", NullaWeb do
|
||||
pipe_through [:browser, :require_authenticated_user]
|
||||
|
||||
live_session :require_authenticated_user,
|
||||
on_mount: [{NullaWeb.UserAuth, :ensure_authenticated}] do
|
||||
live "/users/settings", UserSettingsLive, :edit
|
||||
live "/users/settings/confirm_email/:token", UserSettingsLive, :confirm_email
|
||||
end
|
||||
end
|
||||
|
||||
scope "/", NullaWeb do
|
||||
pipe_through [:browser]
|
||||
|
||||
delete "/users/log_out", UserSessionController, :delete
|
||||
|
||||
live_session :current_user,
|
||||
on_mount: [{NullaWeb.UserAuth, :mount_current_user}] do
|
||||
live "/users/confirm/:token", UserConfirmationLive, :edit
|
||||
live "/users/confirm", UserConfirmationInstructionsLive, :new
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
245
lib/nulla_web/user_auth.ex
Normal file
245
lib/nulla_web/user_auth.ex
Normal file
|
@ -0,0 +1,245 @@
|
|||
defmodule NullaWeb.UserAuth do
|
||||
use NullaWeb, :verified_routes
|
||||
|
||||
import Plug.Conn
|
||||
import Phoenix.Controller
|
||||
|
||||
alias Nulla.Accounts
|
||||
|
||||
# Make the remember me cookie valid for 60 days.
|
||||
# If you want bump or reduce this value, also change
|
||||
# the token expiry itself in UserToken.
|
||||
@max_age 60 * 60 * 24 * 60
|
||||
@remember_me_cookie "_nulla_web_user_remember_me"
|
||||
@remember_me_options [sign: true, max_age: @max_age, same_site: "Lax"]
|
||||
|
||||
@doc """
|
||||
Logs the user in.
|
||||
|
||||
It renews the session ID and clears the whole session
|
||||
to avoid fixation attacks. See the renew_session
|
||||
function to customize this behaviour.
|
||||
|
||||
It also sets a `:live_socket_id` key in the session,
|
||||
so LiveView sessions are identified and automatically
|
||||
disconnected on log out. The line can be safely removed
|
||||
if you are not using LiveView.
|
||||
"""
|
||||
def log_in_user(conn, user, params \\ %{}) do
|
||||
token = Accounts.generate_user_session_token(user)
|
||||
user_return_to = get_session(conn, :user_return_to)
|
||||
|
||||
conn
|
||||
|> renew_session()
|
||||
|> put_token_in_session(token)
|
||||
|> maybe_write_remember_me_cookie(token, params)
|
||||
|> redirect(to: user_return_to || signed_in_path(conn))
|
||||
end
|
||||
|
||||
defp maybe_write_remember_me_cookie(conn, token, %{"remember_me" => "true"}) do
|
||||
put_resp_cookie(conn, @remember_me_cookie, token, @remember_me_options)
|
||||
end
|
||||
|
||||
defp maybe_write_remember_me_cookie(conn, _token, _params) do
|
||||
conn
|
||||
end
|
||||
|
||||
# This function renews the session ID and erases the whole
|
||||
# session to avoid fixation attacks. If there is any data
|
||||
# in the session you may want to preserve after log in/log out,
|
||||
# you must explicitly fetch the session data before clearing
|
||||
# and then immediately set it after clearing, for example:
|
||||
#
|
||||
# defp renew_session(conn) do
|
||||
# preferred_locale = get_session(conn, :preferred_locale)
|
||||
#
|
||||
# conn
|
||||
# |> configure_session(renew: true)
|
||||
# |> clear_session()
|
||||
# |> put_session(:preferred_locale, preferred_locale)
|
||||
# end
|
||||
#
|
||||
defp renew_session(conn) do
|
||||
delete_csrf_token()
|
||||
|
||||
conn
|
||||
|> configure_session(renew: true)
|
||||
|> clear_session()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Logs the user out.
|
||||
|
||||
It clears all session data for safety. See renew_session.
|
||||
"""
|
||||
def log_out_user(conn) do
|
||||
user_token = get_session(conn, :user_token)
|
||||
user_token && Accounts.delete_user_session_token(user_token)
|
||||
|
||||
if live_socket_id = get_session(conn, :live_socket_id) do
|
||||
NullaWeb.Endpoint.broadcast(live_socket_id, "disconnect", %{})
|
||||
end
|
||||
|
||||
conn
|
||||
|> renew_session()
|
||||
|> delete_resp_cookie(@remember_me_cookie)
|
||||
|> redirect(to: ~p"/")
|
||||
end
|
||||
|
||||
@doc """
|
||||
Authenticates the user by looking into the session
|
||||
and remember me token.
|
||||
"""
|
||||
def fetch_current_user(conn, _opts) do
|
||||
{user_token, conn} = ensure_user_token(conn)
|
||||
user = user_token && Accounts.get_user_by_session_token(user_token)
|
||||
assign(conn, :current_user, user)
|
||||
end
|
||||
|
||||
defp ensure_user_token(conn) do
|
||||
if token = get_session(conn, :user_token) do
|
||||
{token, conn}
|
||||
else
|
||||
conn = fetch_cookies(conn, signed: [@remember_me_cookie])
|
||||
|
||||
if token = conn.cookies[@remember_me_cookie] do
|
||||
{token, put_token_in_session(conn, token)}
|
||||
else
|
||||
{nil, conn}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Handles mounting and authenticating the current_user in LiveViews.
|
||||
|
||||
## `on_mount` arguments
|
||||
|
||||
* `:mount_current_user` - Assigns current_user
|
||||
to socket assigns based on user_token, or nil if
|
||||
there's no user_token or no matching user.
|
||||
|
||||
* `:ensure_authenticated` - Authenticates the user from the session,
|
||||
and assigns the current_user to socket assigns based
|
||||
on user_token.
|
||||
Redirects to login page if there's no logged user.
|
||||
|
||||
* `:redirect_if_user_is_authenticated` - Authenticates the user from the session.
|
||||
Redirects to signed_in_path if there's a logged user.
|
||||
|
||||
## Examples
|
||||
|
||||
Use the `on_mount` lifecycle macro in LiveViews to mount or authenticate
|
||||
the current_user:
|
||||
|
||||
defmodule NullaWeb.PageLive do
|
||||
use NullaWeb, :live_view
|
||||
|
||||
on_mount {NullaWeb.UserAuth, :mount_current_user}
|
||||
...
|
||||
end
|
||||
|
||||
Or use the `live_session` of your router to invoke the on_mount callback:
|
||||
|
||||
live_session :authenticated, on_mount: [{NullaWeb.UserAuth, :ensure_authenticated}] do
|
||||
live "/profile", ProfileLive, :index
|
||||
end
|
||||
"""
|
||||
def on_mount(:mount_current_user, _params, session, socket) do
|
||||
{:cont, mount_current_user(socket, session)}
|
||||
end
|
||||
|
||||
def on_mount(:ensure_authenticated, _params, session, socket) do
|
||||
socket = mount_current_user(socket, session)
|
||||
|
||||
if socket.assigns.current_user do
|
||||
{:cont, socket}
|
||||
else
|
||||
socket =
|
||||
socket
|
||||
|> Phoenix.LiveView.put_flash(:error, "You must log in to access this page.")
|
||||
|> Phoenix.LiveView.redirect(to: ~p"/users/log_in")
|
||||
|
||||
{:halt, socket}
|
||||
end
|
||||
end
|
||||
|
||||
def on_mount(:redirect_if_user_is_authenticated, _params, session, socket) do
|
||||
socket = mount_current_user(socket, session)
|
||||
|
||||
if socket.assigns.current_user do
|
||||
{:halt, Phoenix.LiveView.redirect(socket, to: signed_in_path(socket))}
|
||||
else
|
||||
{:cont, socket}
|
||||
end
|
||||
end
|
||||
|
||||
defp mount_current_user(socket, session) do
|
||||
Phoenix.Component.assign_new(socket, :current_user, fn ->
|
||||
if user_token = session["user_token"] do
|
||||
Accounts.get_user_by_session_token(user_token)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Used for routes that require the user to not be authenticated.
|
||||
"""
|
||||
def redirect_if_user_is_authenticated(conn, _opts) do
|
||||
if conn.assigns[:current_user] do
|
||||
conn
|
||||
|> redirect(to: signed_in_path(conn))
|
||||
|> halt()
|
||||
else
|
||||
conn
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Used for routes that require the user to be authenticated.
|
||||
|
||||
If you want to enforce the user email is confirmed before
|
||||
they use the application at all, here would be a good place.
|
||||
"""
|
||||
def require_authenticated_user(conn, _opts) do
|
||||
if conn.assigns[:current_user] do
|
||||
conn
|
||||
else
|
||||
conn
|
||||
|> put_flash(:error, "You must log in to access this page.")
|
||||
|> maybe_store_return_to()
|
||||
|> redirect(to: ~p"/users/log_in")
|
||||
|> halt()
|
||||
end
|
||||
end
|
||||
|
||||
def fetch_api_user(conn, _opts) do
|
||||
if Mix.env() != :test do
|
||||
with ["Bearer " <> token] <- get_req_header(conn, "authorization"),
|
||||
{:ok, user} <- Accounts.fetch_user_by_api_token(token) do
|
||||
assign(conn, :current_user, user)
|
||||
else
|
||||
_ ->
|
||||
conn
|
||||
|> send_resp(:unauthorized, "No access for you")
|
||||
|> halt()
|
||||
end
|
||||
else
|
||||
conn
|
||||
end
|
||||
end
|
||||
|
||||
defp put_token_in_session(conn, token) do
|
||||
conn
|
||||
|> put_session(:user_token, token)
|
||||
|> put_session(:live_socket_id, "users_sessions:#{Base.url_encode64(token)}")
|
||||
end
|
||||
|
||||
defp maybe_store_return_to(%{method: "GET"} = conn) do
|
||||
put_session(conn, :user_return_to, current_path(conn))
|
||||
end
|
||||
|
||||
defp maybe_store_return_to(conn), do: conn
|
||||
|
||||
defp signed_in_path(_conn), do: ~p"/"
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue